Я создаю простое приложение с вкладками (3 элемента панели вкладок), используя Swift 5 и Xcode 11. Внутри приложения iOS, в котором изначально было 2 элемента панели вкладок, я думал о создании некоторых тем для своего приложения, и так я сделал, добавив еще один элемент панели вкладок в мое приложение. Я добавил темы, чтобы пользователи могли выбирать вот так, используя UITableViewCells и программно сделанные кнопки:
Вот так выглядит. По крайней мере, для iPhone с большим экраном. Начиная с iPhone 6, все работает нормально, как показано выше. Но ниже iPhone 6, как это iPhone 5, левый набор кнопок исчезает:
Это странно, и я понятия не имею, почему правильный набор кнопок остался.
Это мой UITableViewController:
import UIKit
@objcMembers class CustomViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
self.tableView.rowHeight = 138
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
return "Themes"
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return SingletonViewController.themes.count
}
// 3
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "themeCell", for: indexPath) as! ThemeCell
var cellyi = UIButton(frame: CGRect(x: 5, y: 5, width: 50, height: 30))
cellyi.tag = indexPath.row + 1
/////////
if cellyi.tag == 1 {
let cellButton1 = UIButton(frame: CGRect(x: 0, y: 5, width: 88, height: 119.5))
cellButton1.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(cellButton1)
cell.accessoryView = cellButton1
cellButton1.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10).isActive = true
cellButton1.topAnchor.constraint(equalTo: cell.topAnchor, constant: 10).isActive = true
cellButton1.widthAnchor.constraint(equalToConstant: 88).isActive = true
cellButton1.heightAnchor.constraint(equalToConstant: 119.5).isActive = true
cellButton1.addTarget(self, action: #selector(CustomViewController.backBTN(sender:)), for: .touchUpInside)
cellButton1.setTitle("Classic", for: .normal)
cellButton1.setTitleColor(UIColor.black, for: .normal)
cellButton1.tag = indexPath.row + 1
}else{
let cellButton = UIButton(frame: CGRect(x: 0, y: 5, width: 88, height: 119.5))
cellButton.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(cellButton)
cell.accessoryView = cellButton
cellButton.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10).isActive = true
cellButton.topAnchor.constraint(equalTo: cell.topAnchor, constant: 10).isActive = true
cellButton.widthAnchor.constraint(equalToConstant: 88).isActive = true
cellButton.heightAnchor.constraint(equalToConstant: 119.5).isActive = true
tag = indexPath.row - 1
cellButton.setImage(UIImage(named: SingletonViewController.themes[indexPath.row - 1]), for: UIControl.State.normal)
cellButton.addTarget(self, action: #selector(CustomViewController.backBTN(sender:)), for: .touchUpInside)
cellButton.tag = indexPath.row + 1
}
//////////
cell.addSubview(cellyi)
cell.accessoryView = cellyi
cellyi.backgroundColor = UIColor.red
cellyi.addTarget(self, action: #selector(CustomViewController.backBTN(sender:)), for: .touchUpInside)
if UserDefaults.standard.integer(forKey: "like") == 0{
UserDefaults.standard.set(1, forKey: "like")
}
if UserDefaults.standard.integer(forKey: "like") == indexPath.row + 1{
cellyi.backgroundColor = UIColor.green
if indexPath.row + 1 == 1{
self.tabBarController?.tabBar.barTintColor = UIColor(red:0.84, green:0.84, blue:0.84, alpha:1.0)
self.tabBarController?.tabBar.tintColor = UIColor(red:0.21, green:0.56, blue:0.96, alpha:1.0)
}else if indexPath.row + 1 == 2{
self.tabBarController?.tabBar.barTintColor = UIColor.black
self.tabBarController?.tabBar.tintColor = UIColor(red:0.98, green:0.64, blue:0.02, alpha:1.0)
}
}
tableView.allowsSelection = false
return cell
}
@objc func backBTN(sender: UIButton){
UserDefaults.standard.set(sender.tag, forKey: "like")
tableView.reloadData()
}
}
Я видел некоторые другие подобные вопросы к этому. В настоящее время я до сих пор не нашел решение этой проблемы. Любая помощь приветствуется