在没有导航控制器的情况下将barButtonItems添加到UINavigationBar [英] adding barButtonItems to a UINavigationBar without a Navigation Controller

查看:98
本文介绍了在没有导航控制器的情况下将barButtonItems添加到UINavigationBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向uiNavigationBar添加一些按钮,但它没有附加到导航控制器,我不希望它是。

I am attempting to add a number of buttons to a uiNavigationBar, but it is not attached to a navigation controller, and I don't want it to be.

我试图在栏的左侧添加两个按钮,但我发现的唯一代码示例允许这涉及使用行

I am attempting to add two buttons to the left side of the bar, but the only examples of code I find that allow this involve using the line

 self.navigationItem.leftBarButtonItems

显然,我的UINavigationBar不是navigatinItem。

obviously, my UINavigationBar is not a navigatinItem.

以下是我创建NavBar的方法..

Here is how I have created my NavBar..

.h

@property (nonatomic, retain) UINavigationBar *navBar;

.m

_navBar = [[UINavigationBar alloc] init];
[_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
[self.view addSubview:_navBar];

我已经看到两种方法将项目推送到导航栏但不起作用。

I have seen two ways to push items to a nav bar but neither work.

首先是..

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                  target:self
                                  action:@selector(buttonSettingClicked)];

    [[self navigationItem] setLeftBarButtonItem:barButton]

     self.navigationItem.leftBarButtonItems = _ArrayOfBarButtons;

没有人产生任何结果......我怀疑因为我的UINavigationBar在技术上不是一个'导航项目。 '

neither one produces any results... i suspect because my UINavigationBar is not technically a 'navigation item.'

那么如何将项目添加到我的NavigationBar?

So how can I add items to my NavigationBar?

推荐答案

您需要首先创建UINavigationItem,向其添加按钮,然后将navigationItem添加到导航栏。

You need to create the UINavigationItem first, add the button to it, and then add the navigationItem to the navigation bar.

[super viewDidLoad];
    _navBar = [[UINavigationBar alloc] init];
    [_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
    [self.view addSubview:_navBar];
    UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(buttonSettingClicked)];
    [navItem setLeftBarButtonItem:barButton];

    [_navBar setItems:@[navItem]];

这篇关于在没有导航控制器的情况下将barButtonItems添加到UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆