How can I set two buttons on the right side of an UINavigationBar?

我只是一个虾纸丫 提交于 2019-12-18 09:39:49

问题


I have made a standard Master-Detail application (with the Xcode template). I modified it, and now when I click one cell of the first table view the app shows another table view (which is the detail because is filtered by the clicked cell value). In a standard table view I would have this situation on the UINavigationBar:

Edit Table title + (for adding new items)

I need the default back button of UINavigationBar too. Is it possible and allowed to do this? Graphically it would be like this:

< Back Table title Edit +

Do you know any other layout to display 3 button (back - edit - add) plus title all in the top bar?

Sorry for not posting images, Thanks in advance.

P.S. I'd like to know if it is possible to do the things I'm asking with built-in back button and edit button (I mean those that the system puts automatically to the navigation bar).


回答1:


you need to setup array of right bar items to set up bar buttons right side...

Here is the code of 2 custom button to setup bar button items right side...you can use default button instead of this

UIButton *btnabout =  [UIButton buttonWithType:UIButtonTypeCustom];
btnabout.frame = CGRectMake(0,8,30,30);
[btnabout setBackgroundImage:[UIImage imageNamed:@"about.png"] forState:UIControlStateNormal];
[btnabout addTarget:self action:@selector(callselector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *about = [[UIBarButtonItem alloc] initWithCustomView:btnabout];
about.tintColor = [UIColor whiteColor];


UIButton *btnsetting =  [UIButton buttonWithType:UIButtonTypeCustom];
btnsetting.frame = CGRectMake(0,8,30,30);
[btnsetting setBackgroundImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
[btnsetting addTarget:self action:@selector(SettingData) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *setting = [[UIBarButtonItem alloc] initWithCustomView:btnsetting];
setting.tintColor = [UIColor whiteColor];

self.navigationItem.rightBarButtonItems =[NSArray arrayWithObjects:setting, about, nil];
// --- or if you want left side ----
//  self.navigationItem.leftBarButtonItems =[NSArray arrayWithObjects:setting, about, nil];



回答2:


[self.navigationItem setRightBarButtonItems:@[/*array of UIBarButtonItem*/] animated:NO]

Apple doc




回答3:


to someone stuck with this problem in swift, here is the correct answer :

let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)


来源:https://stackoverflow.com/questions/31612471/how-can-i-set-two-buttons-on-the-right-side-of-an-uinavigationbar

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