How do you add more than one UIBarButton on UINavigationItem.rightBarButtonItem (or leftBarButtonItem)?

為{幸葍}努か 提交于 2019-11-27 18:24:50

To get rid of the background ('seam') of a UIToolbar, create a subclass of UIToolbar and override the (void)drawRect:(CGRect)rect method. Leave that blank and your UIToolbar will no longer have a background.

Just used this in my own project and worked great. Found this in the comments of: http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

iOS 5.0 now supports multiple buttons. See the iOS documentation for UINavigationItem. Specifically, the following:

Properties:

@property(nonatomic, copy) NSArray *leftBarButtonItems;
@property(nonatomic, copy) NSArray *rightBarButtonItems;
@property BOOL leftItemsSupplementBackButton;

Methods:

- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;

I posted code to add two buttons to the right of the navigationBar. You can set barStyle = -1 instead of subclassing UIToolbar.

UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myWidth, myHeight)];
// make UIView customView1... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView1];
[customView1 release];
// make UIView customView2... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView2];
[customView2 release];
UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];
[parentView release];
self.navigationItem.rightBarButtonItem = customBarButtomItem;
[customBarButtomItem release];

see uicatalogue example available at apple's site for free...they used uisegmented control to show three buttons in place of right bar button on navigaion bar...

I can't comment but in addition to @iworkinprogress I had to set the UIToolbar background color to clear:

    [toolbar setBackgroundColor:[UIColor clearColor]];

This was also found in the comments of http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html.

In iOS 4.x the clearColor seems to have no effect on the UIToolbar, whereas overriding its drawRect: did.

I came up with a helper function I'm using all over my project. Basically it checks if there is already a button on the bar and either add the new one or merge it with existing buttons. So you can call the function just once or multiple times:

+ (void)AddButtonToBar:(UIViewController *)controller withImage:(NSString *)imageName withAction:(SEL)action withFrame:(CGRect) frame{
    UIButton *newButton =[[UIButton alloc] init];
    [newButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    newButton.frame = frame;
    [newButton addTarget:controller action:action forControlEvents:UIControlEventTouchUpInside];

    if ([[controller.navigationItem rightBarButtonItems] count] == 0)
        [controller.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
    else {
        NSMutableArray *existingButtons = [[NSMutableArray alloc] initWithArray:[controller.navigationItem rightBarButtonItems]];
        [existingButtons addObject:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
        [controller.navigationItem setRightBarButtonItems:(NSArray *)existingButtons];
    }
}

Call it from the view controller:

[Helper AddButtonToBar:self withImage:@"imageName.png" withAction:@selector(myAction) withFrame:CGRectMake(0, 0, 24, 24)];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!