简体   繁体   中英

iOS 6 - UIBarButtonItem setStyle not working

I have a UIBarButtonItem in a UIToolBar . I have set up the code like so:

UIBarButtonItem *barbutton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionSheet:)];
[barbutton setStyle:UIBarButtonItemStyleBordered];
[toolBar setItems:[NSArray arrayWithObjects: [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], barButton, nil]] ;
[toolBar setClipsToBounds:YES];

I am expecting the barButton to show with a border, but only the built-in action icon shows up. Any idea what I might be missing?

On searching around for this issue, I found out that the style that you want - UIBarButtonItemStyleBordered gets applied when you init the UIBarButtonItem using initWithImage only.

So, maybe you could get an image for the Action button and try this

UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithImage:@"action.png" style:UIBarButtonItemStyleBordered target:nil action:nil];
[barbutton setStyle:UIBarButtonItemStyleBordered];

Tell me if this works for you. Cheers!


Edit

Upon testing your own code, it seems to be working fine for me. The line

 [barbutton setStyle:UIBarButtonItemStyleBordered]; 

will make the action buttion like this

在此处输入图片说明

and on commenting the above line out, I am getting a button like this

在此处输入图片说明

Isn't that what you want?

In iOS 6 you can use the new method of UIBarButtonItem class:

- (void)setBackgroundImage:(UIImage *)backgroundImage
              forState:(UIControlState)state
                 style:(UIBarButtonItemStyle)style
            barMetrics:(UIBarMetrics)barMetrics

It sets the background image for the specified state, style, and metrics. More details are available in the [Apple docs][1]

Hopefully it will help you.

use following code for ios 6 issue. It will be OK on IOS 7 as well.

UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:@selector(actionMethod) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];

UIBarButtonItem *rightButtonItem  = [[UIBarButtonItem alloc] initWithCustomView:btton];

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