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.
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?
2
Answers
I’m not sure this is what you are looking for or not.
and one more thing instead of using
Use the button to change property.
That will likely not be a suitable approach.
A
UIButton
does various things with itstitleLabel
– 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:
TestButton
does get the appearanceThis is the same reason why we use:
instead of:
You probably want to configure those properties in your custom
TestButton
class. Here’s the basics: