Search code examples
iphoneiosobjective-cuisplitviewcontrolleruibarbuttonitem

How to add 2 buttons on navigation bar?


I am using Split View Controller, which has 2 View controllers on the second view controller i am suppose add two buttons on the right side of navigation controller. i have used the following code to add one button which works:

UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
    [barButton setCustomView:btnShare];
    self.navigationItem.rightBarButtonItem=barButton;

tried this link http://osmorphis.blogspot.in/2009/05/multiple-buttons-on-navigation-bar.html but could not succeed.Please help me fix this.


Solution

  • Try this

    NSMutableArray *arrRightBarItems = [[NSMutableArray alloc] init];
    UIButton *btnSetting = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnSetting setImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal];
    btnSetting.frame = CGRectMake(0, 0, 32, 32);
    btnSetting.showsTouchWhenHighlighted=YES;
    [btnSetting addTarget:self action:@selector(onSettings:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSetting];
    [arrRightBarItems addObject:barButtonItem];
    
    UIButton *btnLib = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLib setImage:[UIImage imageNamed:@"library.png"] forState:UIControlStateNormal];
    btnLib.frame = CGRectMake(0, 0, 32, 32);
    btnLib.showsTouchWhenHighlighted=YES;
    [btnLib addTarget:self action:@selector(onMyLibrary:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:btnLib];
    [arrRightBarItems addObject:barButtonItem2];
    
    UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnRefresh setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
    btnRefresh.frame = CGRectMake(0, 0, 32, 32);
    btnRefresh.showsTouchWhenHighlighted=YES;
    [btnRefresh addTarget:self action:@selector(onRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnRefresh];
    
    [arrRightBarItems addObject:barButtonItem1];
    self.navigationItem.rightBarButtonItems=arrRightBarItems;