且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UIBarButtonItem突出显示的颜色

更新时间:2022-10-15 23:38:59

From what I remember from facing a similar issue, UINavigationBar will just take the tintColor and make it darker for the UIBarButtonItem (unless the style is set to BarStyleBlack, in which case it makes it a dull gray).

To do what you ask, I would create a custom UIButton with background images for the different control states that match your color scheme, then use this UIButton as the view for a custom UIBarButtonItem.

UIButton *customButton = [UIButton buttonWithType:...];

//normal_button.png and selected_button.png need to be created by you
[customButton setBackgroundImage: [UIImage imageNamed:@"normal_button.png"] forState:UIControlStateNormal];
[customButton setBackgroundImage: [UIImage imageNamed:@"selected_button.png"] forState:UIControlStateSelected];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView: customButton];

If you want to try and encapsulate this, you could always create a factory or a custom init method on UIBarButtonItem (via a category) and use the above code (with slight modifications).

I am aware that I am not fully addressing your second point on just overriding the control state with a category. I don't know what method to override in UIBarButtonItem to do such a thing, but you may be able to accomplish what you want via method swizzling (http://cocoadev.com/index.pl?MethodSwizzling) once you find out which method you want to exchange.

I should note that I've only ever used swizzling for testing/debugging.

相关阅读

技术问答最新文章