我有一个侧面菜单滑出来显示一个表视图,从那里我有segues使用显示视图控制器 . segue必须直接连接到视图控制器;我不能使用导航控制器 .
如何在没有导航控制器的情况下添加带有条形按钮项的导航栏?
虽然有几种聪明的方法可以回答你的问题 . 我刚刚以编程方式解决了它,并在我的 viewWillAppear 中编写了以下代码(注意 - viewDidLoad 也没关系,但不建议) -
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]; }
因此,您有一个带有蓝色条形按钮项目的白色导航栏,没有导航控制器 . 同样,在您的情况下还有其他方法可以实现它 . 希望,这很有帮助 .
输出 -
Update -
要添加图片 -
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];
有一种方法可以在界面构建器中使用 NavigationItem .
NavigationItem
首先在界面构建器中将 NavigationItem 添加到 ViewController ,就像使用 NavigationController 一样 . 确保通过在模拟指标下选择 Inferred 和 None 以外的其他内容使 NavigationBar 可见 .
ViewController
NavigationController
Inferred
None
NavigationBar
其次,在 viewDidLoad 中,只需添加以下行:
- (void)viewDidLoad { [super viewDidLoad]; UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame: frame]; bar.items = @[self.navigationItem]; [self.view addSubview: bar]; }
至于 frame , width 将与 ViewController 相同, height 将是 44.0 或 64.0 ,具体取决于 status bar 是否可见 .
frame
width
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);
如果你想支持 different orientations ,请使用 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], ]];
Swift 4 Version
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.rightBarButtonItem = cancelButton navigationItem.leftBarButtonItem = doneButton navigationBar.items = [navigationItem]
添加“viewWillAppear”要非常小心,因为这个方法可以被调用更多次(例如,模态出现....)所以使用一个懒惰的方法:
1)声明一个var:
var myNav: UINavigationBar?
2)测试是否已经设置:
viewWillAppear:(BOOL)animated { if self.myNav != nil{ return } self.myNav = ....
3)确保删除退出的控制器,例如在didDisappear上...
note ..指定大小不正确..如果iOS旋转,它不能正常工作..
5 回答
虽然有几种聪明的方法可以回答你的问题 . 我刚刚以编程方式解决了它,并在我的
viewWillAppear
中编写了以下代码(注意 -viewDidLoad
也没关系,但不建议) -因此,您有一个带有蓝色条形按钮项目的白色导航栏,没有导航控制器 . 同样,在您的情况下还有其他方法可以实现它 . 希望,这很有帮助 .
输出 -
Update -
要添加图片 -
有一种方法可以在界面构建器中使用
NavigationItem
.首先在界面构建器中将
NavigationItem
添加到ViewController
,就像使用NavigationController
一样 . 确保通过在模拟指标下选择Inferred
和None
以外的其他内容使NavigationBar
可见 .其次,在
viewDidLoad
中,只需添加以下行:至于
frame
,width
将与ViewController
相同,height
将是44.0
或64.0
,具体取决于status bar
是否可见 .如果你想支持 different orientations ,请使用
NSLayoutConstraints
:Swift 4 Version
添加“viewWillAppear”要非常小心,因为这个方法可以被调用更多次(例如,模态出现....)所以使用一个懒惰的方法:
1)声明一个var:
2)测试是否已经设置:
3)确保删除退出的控制器,例如在didDisappear上...
添加“viewWillAppear”要非常小心,因为这个方法可以被调用更多次(例如,模态出现....)所以使用一个懒惰的方法:
1)声明一个var:
2)测试是否已经设置:
3)确保删除退出的控制器,例如在didDisappear上...
note ..指定大小不正确..如果iOS旋转,它不能正常工作..