我需要有关 UIBarButtonItem
未显示在 UINavigationBar
中的帮助。
我正在尝试将 UINavigationBar
放入我的第三个 View 中。由于我不太确定 UINavigationController
是否有效,我决定手动初始化 UINavigationBar
及其 UINavigationItem
initwithTitle
.该代码有效,但我的问题是添加 UIBarButtonItem
因为它不会显示在 UINavigationBar
中。
- (void)viewDidLoad
{
[super viewDidLoad];
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
[navigationBar pushNavigationItem:navigationItem animated:NO];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(showPI:)];
self.navigationItem.rightBarButtonItem = button;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, self.view.frame.size.height - navigationBar.frame.size.height) style:UITableViewStylePlain];
self.tableView = tableView;
[self.view addSubview:tableView];
[self.view addSubview:navigationBar];
[self showCurrencies];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
我不确定缺少什么。
最佳答案
就您而言,您正在创建自己的导航栏。尽量不要分配 init 你的 navigationItem,而是使用
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *item = [navigationBar.items lastObject];
然后将您创建的 barbuttonItem 设置为项目的 rightBarButtonItem,
item.rightBarButtonItem = button;
这对我有用。
关于ios - UIBarButton 未显示在 UINavigationBar 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16841807/