简体   繁体   English

UIBarButtonItem tintColor iOS4

[英]UIBarButtonItem tintColor iOS4

I am using following code to create a UIBarButtonItem:我正在使用以下代码创建 UIBarButtonItem:

UIBarButtonItem* searchBarButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search_picto"] style:UIBarButtonItemStyleBordered target:self action:@selector(actionSearch:)] autorelease];
searchBarButton.tintColor = [UIColor colorWithRed:0.27 green:0.60 blue:0.20 alpha:1.00];

I add this and another button to a UIToolbar, not sure if that's relevant.我将此和另一个按钮添加到 UIToolbar,不确定这是否相关。 This works fine and gives me the look I intended:这很好用,并给了我我想要的外观:

UIButtonBarItem 截图

The problem is that with iOS 4 (which I have to support) I can't set the tintColor.问题是,在 iOS 4(我必须支持)中,我无法设置 tintColor。 Unrecognized Selector.无法识别的选择器。 How can I create such a button for iOS 4?如何为 iOS 4 创建这样的按钮? I need to be able to set the tintColor and have UIBarButtonItemStyleBordered.我需要能够设置 tintColor 并拥有 UIBarButtonItemStyleBordered。

Thanks for your help.谢谢你的帮助。


I got it to work like this:我让它像这样工作:

UISegmentedControl* searchButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"", nil]];
[searchButton setImage:[UIImage imageNamed:@"search_picto"] forSegmentAtIndex:0];
searchButton.momentary = YES;
searchButton.segmentedControlStyle = UISegmentedControlStyleBar;
searchButton.tintColor = [DELEGATE.config getColor:IFXShopConfigScopeShop name:@"navigationTint"];
[searchButton addTarget:self action:@selector(actionSearch:) forControlEvents:UIControlEventValueChanged];

You have to initialize it with one empty NSString, otherwise you can't set an image.你必须用一个空的 NSString 初始化它,否则你不能设置图像。 Also the addTarget has to use UIControlEventValueChanged此外 addTarget 必须使用UIControlEventValueChanged

tintColor是在 iOS5 中添加的,对于 iOS4 在这里你有一个解决方案

You need to create an UIButton and either draw the button to look like this by yourself or set a background image with this and initialize the UIBarButtonItem with a custom view (your UIButton ).您需要创建一个UIButton并自己绘制按钮以使其看起来像这样,或者使用此设置背景图像并使用自定义视图(您的UIButton )初始化UIBarButtonItem

PS From the release of iOS6 you can't compile to armv6 anymore and the lowest version you can support is 4.3, which according to me isn't worth it... PS 从 iOS6 发布开始,您不能再编译到 armv6,并且您可以支持的最低版本是 4.3,据我所知,这不值得...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM