How to add dynamic/multi button in UINavigation Bar?

放肆的年华 提交于 2019-12-23 04:47:08

问题


How to add dynamic/multi button in UINavigatation Bar?

Like below image:


回答1:


You can use a UIToolBar which then hold hold multiple UIBarButtonItem objects.

- (void)viewDidLoad {
    [super viewDidLoad];




    // create a toolbar to have two buttons in the right

        UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

        // create the array to hold the buttons, which then gets added to the toolbar
        NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

        // create a standard "add" button
        UIBarButtonItem* bi = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
        bi.style = UIBarButtonItemStyleBordered;
        [buttons addObject:bi];
        [bi release];


        // create a standard "refresh" button
        bi = [[UIBarButtonItem alloc]
              initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
        bi.style = UIBarButtonItemStyleBordered;
        [buttons addObject:bi];
        [bi release];

        // stick the buttons in the toolbar
        [tools setItems:buttons animated:NO];

        [buttons release];

        // and put the toolbar in the nav bar
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
        [tools release];

}

-(void)add{
    NSLog(@"Code to add the row in tableview");
}

-(void)refresh {
    NSLog(@"code to refresh the tableView");
}



回答2:


I don't shore about iOS5, but in iOS4 you can do this only sublussing (or even making your own) nav. bar in reason to make it custom.



来源:https://stackoverflow.com/questions/8381665/how-to-add-dynamic-multi-button-in-uinavigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!