我正在尝试以编程方式将UIButtonBarItem添加到UINavigationBar。我有一个Topbar类,该类在我的某些ViewControllers上导入,然后由[self.view addSubview:[Topbar insertTopbar]];
调用
#import "Topbar.h"
@implementation Topbar
+(UINavigationBar*)insertTopbar
{
UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navTitle = [UINavigationItem alloc];
navTitle.title = @"MyApp";
UINavigationItem *navAdd = [UINavigationItem alloc];
navAdd.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(addFood)];
[navBar setItems:@[navAdd, navTitle] animated:false];
return navBar;
}
@end
但是,当我尝试使用
navBar setItems:@[navAdd, navTitle]
设置商品时,它会显示“返回”按钮如果然后我忽略了
navTitle
,因此它读取navBar setItems:@[navAdd]
,则会突然出现“+”号。似乎“navTitle”已插入第一项。 最佳答案
尝试以下方法,做了几个星期,现在对我来说很好! :)
-(UINavigationBar*)insertTopbar
{
UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navAdd = [UINavigationItem alloc];
navAdd.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(addFood)];
[navBar pushNavigationItem:navAdd animated:YES];
navBar.topItem.title = @"MyApp";
return navBar;
}
关于ios - 以编程方式将UIButtonBarItem添加到UINavigationBar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22387011/