问题描述
我有一个侧向菜单,该菜单可以滑出以显示表格视图,并且从那里可以使用显示视图控制器进行搜索。 segue必须直接连接到视图控制器。我不能使用导航控制器。
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?
推荐答案
虽然有几种聪明的方法可以回答您的问题。我只是以编程方式解决了该问题,并在 viewWillAppear
中编写了以下代码(注意- viewDidLoad
也可以,但不建议使用)-
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];
}
因此,您有一个带有蓝色栏按钮项的白色导航栏,而没有Navigation控制器。同样,您还可以通过其他方法来实现它。希望对您有所帮助。
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.
输出-
更新-
要添加图像-
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];
这篇关于将导航栏添加到没有导航控制器的视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!