UIToolbar tint on iOS 4

白昼怎懂夜的黑 提交于 2019-11-28 07:39:00

(must be frank here - I knew the answer before posting, just didn't know how to load this data to StackOverflow. Thought the solution I found was valuable for others, so wanted to post it here. I'm new here, so please no harsh critics :) )

So eventually the problem resulted from, AFAICT, a change in behavior in the OS.

As stated the tint code worked before the upgrade and was written like this:

// Toolbar content               
NSArray *items=[NSArray arrayWithObjects: ... ]; // PSEUDO CODE HERE
[toolbar setItems:items]; 

// Add tint
toolbar.tintColor = [UIColor colorWithRed:0.83 green:0.43 blue:0.57 alpha:0.5];

What I needed to do, was just reverse the order of things:

// Add tint
toolbar.tintColor = [UIColor colorWithRed:0.83 green:0.43 blue:0.57 alpha:0.5];

// Toolbar content               
NSArray *items=[NSArray arrayWithObjects: ... ]; // PSEUDO CODE HERE
[toolbar setItems:items]; 

(If you created UIToolbar in Interface Builder, you can change it's tint there, and that applies for the buttons as well).

I guess the tint updated all buttons before iOS 4, while in iOS 4 it doesn't and when adding buttons, they check for existing tint. But this is just a guess. The solution works anyhow..

Hope this helps someone, and that I didn't violate any sacred SO rules...

Cheers!

Well, it seems more like an OS bug than a feature, since navigation bars do change their item's color when you set their tintColor.

We've found that if you change the item's style, it refreshes their color as a side effect. Doing the following worked in our case. The original buttons are bordered, so we change them to plain and set them to bordered again. You may do a more complicated and generic code that saves the current style, sets another one and then switchs back. I am just to lazy to do that. :D Anyway, you get the idea.

toolbar.tintColor = //<some dynamically obtained UIColor>

// Workaround to properly set the UIBarButtonItem's tint color in iOS 4
for (UIBarButtonItem * item in toolbar.items)
{
    item.style = UIBarButtonItemStylePlain;
    item.style = UIBarButtonItemStyleBordered;
}

Regards, Rula.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!