Add a navigation bar to a view without a navigation controller(向没有导航控制器的视图添加导航栏)
问题描述
我有一个侧边菜单,可以滑出以显示表格视图,并且从那里我有使用显示视图控制器的 segues.segue 必须直接连接到视图控制器;我不能使用导航控制器.
如何在没有导航控制器的情况下添加带有条形按钮项的导航栏?
虽然有几种聪明的方法可以回答您的问题.我只是以编程方式解决了它并在我的 viewWillAppear 中编写了以下代码(注意 - viewDidLoad 也可以,但不建议) -
因此,您有一个白色导航栏,其中包含蓝色栏按钮项,但没有导航控制器.同样,在您的情况下还有其他方法可以实现它.希望,这对您有所帮助.
输出 -
更新 -
添加图片 -
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,10,32,32)];[myImage setImage:[UIImage imageNamed:@"image.png"]];self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myImage];[self.view addSubview:myImage];I have a side menu that slides out to display a table view and from there I have segues that use the reveal view controller. The segue has to connect directly to the view controller; I can't use a navigation controller.
How do I add a navigation bar with a bar button item without a navigation controller?
While there are several smart ways to answer your question. I just solved it programmatically and wrote the following code in my viewWillAppear (note - viewDidLoad is also okay, but not suggested) -
-(void) viewWillAppear:(BOOL)animated {
UINavigationBar *myNav = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[UINavigationBar appearance].barTintColor = [UIColor lightGrayColor];
[self.view addSubview:myNav];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self action:nil];
UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:@"Navigation Title"];
navigItem.rightBarButtonItem = doneItem;
navigItem.leftBarButtonItem = cancelItem;
myNav.items = [NSArray arrayWithObjects: navigItem,nil];
[UIBarButtonItem appearance].tintColor = [UIColor blueColor];
}
So, you have a white navigation bar with blue bar button items without a Navigation controller. Again, there are other ways to implement it in your case. Hope, this was helpful.
Output -
Update -
To add images -
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,10,32,32)];
[myImage setImage:[UIImage imageNamed:@"image.png"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myImage];
[self.view addSubview:myImage];
这篇关于向没有导航控制器的视图添加导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向没有导航控制器的视图添加导航栏
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
