ios 向没有导航控制器的视图添加导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23859785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a navigation bar to a view without a navigation controller
提问by user3381665
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.
我有一个侧边菜单,可以滑出以显示表格视图,从那里我有使用显示视图控制器的 segue。segue 必须直接连接到视图控制器;我不能使用导航控制器。
How do I add a navigation bar with a bar button item without a navigation controller?
如何在没有导航控制器的情况下添加带有栏按钮项的导航栏?
采纳答案by raurora
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) -
虽然有几种聪明的方法可以回答您的问题。我只是以编程方式解决了它并在我的viewWillAppear
(注意 -viewDidLoad
也可以,但不建议)中编写了以下代码-
-(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];
回答by Aerows
There is a way to use the NavigationItem
in interface builder for this.
有一种方法可以为此使用NavigationItem
界面构建器。
First add a NavigationItem
to your ViewController
in the interface builder, like you would with a NavigationController
. Make sure to make the NavigationBar
is visible by selecting something other than Inferred
and None
under simulated metrics.
首先添加NavigationItem
到您ViewController
在界面生成器,就像你使用一个NavigationController
。确保NavigationBar
通过选择模拟指标之外Inferred
和None
下的其他内容使可见。
Second, in viewDidLoad
, just add the following lines:
其次,在 中viewDidLoad
,只需添加以下几行:
- (void)viewDidLoad
{
[super viewDidLoad];
UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame: frame];
bar.items = @[self.navigationItem];
[self.view addSubview: bar];
}
As for frame
, width
will be the same as your ViewController
and height
will be either 44.0
or 64.0
depending if the status bar
is visible or not.
至于frame
,width
将是一样的ViewController
,并height
会要么44.0
还是64.0
取决于如果status bar
是可见或不可见。
CGFloat navigationBarHeight = 44.f + [UIApplication sharedApplication].statusBarFrame.size.height;
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, navigationBarHeight);
And if you wish to support different orientationsuse NSLayoutConstraints
:
如果您希望支持不同的方向,请使用NSLayoutConstraints
:
CGFloat navigationBarHeight = 44.f + [UIApplication sharedApplication].statusBarFrame.size.height;
[self.view addConstraints: @[
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: bar
attribute: NSLayoutAttributeLeft
multiplier: 1.0
constant: 0.0],
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeRight
relatedBy: NSLayoutRelationEqual
toItem: bar
attribute: NSLayoutAttributeRight
multiplier: 1.0
constant: 0.0],
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeTop
relatedBy: NSLayoutRelationEqual
toItem: bar
attribute: NSLayoutAttributeTop
multiplier: 1.0
constant: 0.0],
[NSLayoutConstraint constraintWithItem: bar
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 1.0
constant: navigationBarHeight],
]];
回答by Vitaly
Swift 4 Version
斯威夫特 4 版本
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
navigationBar.barTintColor = UIColor.lightGray
view.addSubview(navigationBar)
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: nil)
let navigationItem = UINavigationItem(title: "Title")
navigationItem.leftBarButtonItem = cancelButton
navigationItem.rightBarButtonItem = doneButton
navigationBar.items = [navigationItem]
回答by ingconti
be very careful about adding in "viewWillAppear", as this method can be called more times, (for example is a modal appears....) so use a lazy approach:
添加“viewWillAppear”时要非常小心,因为这个方法可以被调用更多次,(例如出现模态......)所以使用懒惰的方法:
1) declare a var:
1)声明一个var:
var myNav: UINavigationBar?
2) test if already set:
2)测试是否已经设置:
viewWillAppear:(BOOL)animated {
if self.myNav != nil{
return
}
self.myNav = ....
3) be sure remove exiting controller, for example on didDisappear...
3) 确保删除现有的控制器,例如在 didDisappear 上...
note.. is not correct to specify size.. if iOS rotates, it does not work fine..
注意.. 指定大小不正确.. 如果 iOS 旋转,它不能正常工作..