1

I'm creating a simple tabbed(3 tab bar items) app using Swift 5 and Xcode 11. Inside the iOS app, which originally had 2 tab bar items, I was thinking of making some themes for my app, and so I did, adding one more tab bar item to my app. I added the themes for the users to select like this, using UITableViewCells and programmatically made buttons:

enter image description here

That's what looks like. At least for big screen sized iPhones. Starting at iPhone 6, everything works fine, like pictured above. But below iPhone 6, like this iPhone 5, the left set of buttons disappear:

enter image description here

It's weird, and I have no idea why the right set of buttons stayed.

This is my 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()
    }

}

I have viewed some other of the similar questions to this one. Currently, I still haven't found a solution to this. Any help is appreciated

KLM
  • 157
  • 9
  • I'm sure that you don't need to add buttons dynamically in your cells. Create cell prototypes with your buttons, use Auto Layout and customize cells using custom cell class. See answers in [creating custom tableview cells in swift](https://stackoverflow.com/questions/24170922/creating-custom-tableview-cells-in-swift) – AlexSmet Jan 21 '20 at 23:38

0 Answers0