ios - UIBarButton 未显示在 UINavigationBar 中

标签 ios uinavigationcontroller uinavigationbar uibarbuttonitem uinavigationitem

我需要有关 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/

相关文章:

ios - 使 UIView 固定位置到 UITableViewController 的顶部

iphone - 如何将我的主页的纬度/经度与 iPhone 中主页图像的像素关联起来?

ios - 关闭和其他 GCD 问题中的 Weak DispatchGroup

ios - preformSegue 后编辑导航栏

objective-c - 废弃内存、泄漏、导航 Controller

ios - Swift——在 App Delegate 中实例化一个没有 Storyboard的导航 Controller

ios - 滑动 ViewController 时导航栏错误

ios - 在 Swift 中重命名 socket

ios - iOS 编程 View 之间的交换/转换

ios - NavigationBar 改变颜色本身