ios - 右栏按钮不出现

标签 ios objective-c

我尝试以编程方式添加 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/

相关文章:

ios - 如何使 Parse Installation 对象的属性成为指向 User 对象的指针?

objective-c - isDaylightSavingTime 返回 NO 尽管我的时区确实遵守夏令时

iphone - IOS 5 SDK 中的 Storyboard 但也针对 IOS 4/3?

iphone - IOS UItableview reloadRowsAtIndexPaths 刷新问题

ios - NSURL 需要很长时间才能返回 URL 的内容。有没有办法加快速度?

iphone - 如何从 UITableViewCell 获取 UITableView?

ios - UITableview 阻塞 uiview

ios - 如何验证URL是正确的,和/或从任意文本中提取有效的URL?

iphone - 当返回的对象也可能从持久存储中删除时,如何处理 fetchedResultsController 返回的错误对象

ios - 删除以前在 Swift 中从同一个应用程序存储的健康数据?