【问题标题】:Add a navigation bar to a view without a navigation controller向没有导航控制器的视图添加导航栏
【发布时间】:2014-07-14 14:48:31
【问题描述】:

我有一个侧边菜单,可以滑出以显示表格视图,并且从那里我有使用显示视图控制器的 segues。 segue 必须直接连接到视图控制器;我不能使用导航控制器。

如何在没有导航控制器的情况下添加带有条形按钮项的导航栏?

【问题讨论】:

    标签: ios cocoa-touch uinavigationcontroller storyboard uinavigationbar


    【解决方案1】:

    Swift 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]
    

    【讨论】:

      【解决方案2】:

      添加“viewWillAppear”时要非常小心,因为这个方法可以调用更多次,(例如出现模态......)所以使用惰性方法:

      1) 声明一个变量:

      var myNav: UINavigationBar?
      

      2) 测试是否已经设置:

      viewWillAppear:(BOOL)animated {
      if self.myNav != nil{
           return
      }
      self.myNav = ....
      

      3) 确保移除退出的控制器,例如在 didDisappear...

      注意..指定大小不正确..如果iOS旋转,它不能正常工作..

      【讨论】:

        【解决方案3】:

        有一种方法可以在界面生成器中使用NavigationItem

        首先在界面构建器中将NavigationItem 添加到您的ViewController,就像使用NavigationController 一样。通过在模拟指标下选择InferredNone以外的其他内容,确保NavigationBar可见。

        其次,在viewDidLoad 中,只需添加以下几行:

        - (void)viewDidLoad 
        {
            [super viewDidLoad];
        
            UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame: frame];
            bar.items = @[self.navigationItem];
            [self.view addSubview: bar];
        }
        

        至于framewidth 将与您的ViewController 相同,而height 将是44.064.0,具体取决于status bar 是否可见 em>。

        CGFloat navigationBarHeight = 44.f + [UIApplication sharedApplication].statusBarFrame.size.height;
        CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, navigationBarHeight);
        

        如果您希望支持不同方向,请使用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],
                                     ]];
        

        【讨论】:

          【解决方案4】:

          虽然有几种聪明的方法可以回答您的问题。我只是以编程方式解决了它并在我的viewWillAppear 中编写了以下代码(注意 - viewDidLoad 也可以,但不建议使用)-

          -(无效)viewWillAppear:(BOOL)动画{ UINavigationBar *myNav = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; [UINavigationBar 外观].barTintColor = [UIColor lightGrayColor]; [self.view addSubview:myNav]; UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 样式:UIBarButtonItemStyleBordered 目标:自己 行动:无]; UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" 样式:UIBarButtonItemStyleBordered 目标:自我行动:无]; UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:@"导航标题"]; navigItem.rightBarButtonItem = doneItem; navigItem.leftBarButtonItem = 取消项目; myNav.items = [NSArray arrayWithObjects: navigItem,nil]; [UIBarButtonItem 外观].tintColor = [UIColor blueColor]; }

          因此,您有一个白色导航栏,其中包含蓝色栏按钮项,但没有导航控制器。同样,在您的情况下还有其他方法可以实现它。希望,这对您有所帮助。

          输出 -

          更新 -

          添加图片 -

          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];
          

          【讨论】:

          • 你好,感谢这个伟大的解决方案,但你知道如何有一个图像按钮,它说取消,而不仅仅是文本?
          • 更新了我的答案。嘿,你让我写了很多代码。我敢肯定,您可以自定义代码并使其正常运行。
          • 对不起那个人,我是 Android 的新手,我只是在制作这个 IOS 应用程序,因为我的团队的其他成员都离开了。
          • 我一直听说,当您从 iOS 或 Android 跳转到另一个时,过渡会更容易。无论如何,祝你好运!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-06-15
          • 1970-01-01
          • 1970-01-01
          • 2013-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多