我尝试以编程方式添加UINavigationBar并设置条形按钮项。 我试过了:
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:@"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.leftBarButtonItem = bbiDone;
navItem.rightBarButtonItem = bbiTry;
self.artificialNavBar.items = @[ navItem ];
[self.view addSubview:self.artificialNavBar];
但是,只显示左边的按钮,右边的一个是隐藏的。我错过了什么吗?
答案 0 :(得分:1)
声明
@property (nonatomic, strong) UINavigationItem *navItem;
在此类的.h文件中,您已编写了所有这些内容,然后使用以下代码替换您的代码。
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.artificialNavBar];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:@"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navItem = [[UINavigationItem alloc] init];
[self.navItem setLeftBarButtonItem:bbiDone animated:NO]
[self.navItem setRightBarButtonItem:bbiTry animated:NO]
[self.artificialNavBar setItems:@[self.navItem] animated:NO]
并确保导航栏非原子且强大。