iosswiftuiappearance

Why does UIButton Label ingnores appearance setting when contained in UITableView tableHeaderView?


I have created a simple UIViewController which contains a UITableView. When adding a tableHeaderView which includes a UIButton it seems not to be possible to change the button label using appearance settings. Other buttons outside the tableHeaderView are updated correctly.

enter image description here enter image description here

Here the button which was placed directly in the ViewControllers view is updated correctly while the appearance settings seem not to effect the button inside the tableHeaderView.

class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableHeaderView: UIView!
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        UILabel.appearance(whenContainedInInstancesOf: [TestButton.self]).font = UIFont.systemFont(ofSize: 30)
        UILabel.appearance(whenContainedInInstancesOf: [TestButton.self]).textColor = .red
        
        super.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.tableHeaderView = tableHeaderView
    }
    
    ...
}

class TestButton: UIButton {}

There are now other appearance settings in the project which might explain this. Moving the appearance settings to some other place, e.g. application:didFinishLaunchingWithOptions does not change the problem.

Is this a bug or am I missing something?


Solution

  • That will likely not be a suitable approach.

    A UIButton does various things with its titleLabel - including changing the text color.

    Trying to use UILabel.appearance(...) will not override the normal behavior.

    You can see this by running your code as-is:

    This is the same reason why we use:

    button.setTitleColor(.red, for: .normal)
    

    instead of:

    button.titleLabel?.textColor = .red
    

    You probably want to configure those properties in your custom TestButton class. Here's the basics:

    class TestButton: UIButton {
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            commonInit()
        }
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            commonInit()
        }
        func commonInit() {
            setTitleColor(.red, for: .normal)
            setTitleColor(.lightGray, for: .highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: 30)
            backgroundColor = .systemBlue
            layer.cornerRadius = 6
        }
    }