简体   繁体   中英

UIBarButtonItem tintColor iOS4

I am using following code to create a 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. 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. Unrecognized Selector. How can I create such a button for iOS 4? I need to be able to set the tintColor and have 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. Also the addTarget has to use 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 ).

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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