我尝试以编程方式添加 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];
但是,仅出现左侧栏按钮,右侧栏按钮被隐藏。我错过了什么吗?
最佳答案
声明
@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]
并确保导航栏是非原子的和强大的。
关于ios - 右栏按钮不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39555300/