Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appearanceWhenContainedIn not working as expected with UITableView and UINavigationController

I created a simple project with a UINavigationController, a UIBarButtonItem on the right, and a UITableView. For testing, I want to use appearanceWhenContainedIn: to color the UIBarButtonItems on the UINavigationController orange while also coloring UIButtons in the UITableView red. Alone, each styling works correctly. But when I use both together, it doesn't work. Why??

Scenario 1 (Color UIBarButtonItem in UINavigationController):

[[UIBarButtonItem 
    appearanceWhenContainedIn:[UINavigationController class], nil] 
        setTintColor:[UIColor orangeColor]];

enter image description here


Scenario 2 (Color UIButton in UITableView):

[[UIButton 
    appearanceWhenContainedIn:[UITableView class], nil] 
        setTintColor:[UIColor redColor]];

enter image description here


Scenario 3 (Combine them):

[[UIButton 
    appearanceWhenContainedIn:[UITableView class], nil] 
        setTintColor:[UIColor redColor]];
[[UIBarButtonItem 
    appearanceWhenContainedIn:[UINavigationController class], nil] 
        setTintColor:[UIColor orangeColor]];

enter image description here


Sample project can be downloaded via this link: https://www.dropbox.com/s/k7nabaqc9oso907/AppBugTesting.zip

Update: As I continue to diagnose the problem, I found that if I use "setBackgroundColor" of the UIButton to red rather than setTintColor, it shows the red background and the orange tint to the UIBarButtonItem. For some reason tinting both doesn't work... still not sure why.

like image 623
Kivak Wolf Avatar asked Mar 15 '26 21:03

Kivak Wolf


2 Answers

This works:

[[UIButton 
    appearanceWhenContainedIn:[UITableView class], nil] 
        setTintColor:[UIColor redColor]];
[[UINavigationBar 
    appearanceWhenContainedIn:[UINavigationController class], nil] 
        setTintColor:[UIColor orangeColor]];

Of course this might turn some other things orange that you didn't intend.

like image 70
matt Avatar answered Mar 17 '26 17:03

matt


I would create "empty" UITableView and UINavigationController subclasses to refer to, so as to avoid accidentally changing the appearance of other buttons.

[[UIButton 
    appearanceWhenContainedIn:[MyTableView class], nil] 
        setTintColor:[UIColor redColor]];
[[UINavigationBar 
    appearanceWhenContainedIn:[MyNavigationController class], nil] 
        setTintColor:[UIColor orangeColor]];
like image 37
Rivera Avatar answered Mar 17 '26 17:03

Rivera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!