Search code examples
iosswiftuikituitabbar

Customise TableViewController of uitabbar overflow items in Swift


UIKit automatically manages the overflow items from the tabbar. When you select "More" the overflow items are displayed in a TableViewController. I am struggling to find documentation or previous answers detailing how to programatically customise this TableViewController (for example to fit the style of your application)? Mainly I am not sure how to access this TableViewController in order to then customise.

Example:

enter image description here


Solution

  • You can try something like the code below. Try to get the topViewController's view of moreNavigationController as UITableView and set your custom UITableViewDelegate to it. In the delegate, you can override the WillDisplay cell function to customize the cell.

    class ViewController: UITabBarController {
        override func viewDidLoad() {
            super.viewDidLoad()
            if let moreListViewController = self.moreNavigationController.topViewController {
                if let moreListTableView = moreListViewController.view as? UITableView {
                    moreListTableView.delegate = self
                }
            }
        }
    }
    
    extension ViewController: UITableViewDelegate {
        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            cell.backgroundColor = UIColor.random
        }
    }
    
    private extension UIColor {
        static var random: UIColor {
            return .init(hue: .random(in: 0...1), saturation: 1, brightness: 1, alpha: 1)
        }
    }