【问题标题】:How to add a button to UINavigationBar?如何向 UINavigationBar 添加按钮?
【发布时间】:2011-01-31 01:16:38
【问题描述】:

如何以编程方式向 UINavigationBar 添加按钮?

【问题讨论】:

    标签: ios iphone xcode interface-builder uinavigationbar


    【解决方案1】:

    NavigationBar 上设置rightbutton 的示例代码。

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
        style:UIBarButtonItemStyleDone target:nil action:nil];
    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
    item.rightBarButtonItem = rightButton;
    item.hidesBackButton = YES;
    [bar pushNavigationItem:item animated:NO];
    

    但通常你会有一个NavigationController,让你可以写:

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
        style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.rightBarButtonItem = rightButton;
    

    【讨论】:

    • 我收到关于样式的警告:参数 -> 警告:语义问题:从枚举类型“UIBarButtonSystemItem”隐式转换为不同的枚举类型“UIBarButtonItemStyle”
    • 这应该是 initWithBarButtonSystemItem:UIBarButtonSystemItemDone 以避免警告。
    • 在示例中,我不明白“bar”的来源。 UINavigationItem 的默认顶部栏属性是什么?
    • 答案中的 bar 变量是任何不受导航控制器管理的导航栏。如果你有一个导航控制器,它有一个它自己管理的导航栏——在这种情况下,你推送到导航控制器的每个视图控制器都应该配置它自己的导航项(包括在它自己的 navigationItem 中设置 rightBarButtonItem属性)。
    • 对于当前的读者,我认为没有任何理由在 ARC 下调用 [rightbutton release](在最初撰写此评论时还没有)。
    【解决方案2】:

    上面的答案很好,但我想用一些提示来充实它们:

    如果您想修改后退按钮的标题(导航栏左侧的箭头-y),您必须在 PREVIOUS 视图控制器中进行,而不是在显示它的视图控制器中进行。这就像在说“嘿,如果你在这个视图控制器上推另一个视图控制器,请调用返回按钮“返回”(或其他)而不是默认按钮。”

    如果您想在特殊状态下隐藏后退按钮,例如在显示 UIPickerView 时,请使用 self.navigationItem.hidesBackButton = YES; 并记住在您离开特殊状态时将其设置回来。

    如果要显示特殊符号按钮之一,请使用 initWithBarButtonSystemItem:target:action 形式,其值类似于 UIBarButtonSystemItemAdd

    请记住,该符号的含义取决于您,但请注意人机界面指南。使用 UIBarButtonSystemItemAdd 表示删除项目可能会导致您的应用程序被拒绝。

    【讨论】:

      【解决方案3】:

      将自定义按钮添加到导航栏(带有 buttonItem 的图像并指定操作方法 (void)openView{} 和)。

      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      button.frame = CGRectMake(0, 0, 32, 32);
      [button setImage:[UIImage imageNamed:@"settings_b.png"] forState:UIControlStateNormal];
      [button addTarget:self action:@selector(openView) forControlEvents:UIControlEventTouchUpInside];
      
      UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
      [barButton setCustomView:button];
      self.navigationItem.rightBarButtonItem=barButton;
      
      [button release];
      [barButton release];
      

      【讨论】:

        【解决方案4】:

        下面的示例将在右侧的导航栏上显示一个标题为“联系人”的按钮。它的动作从视图控制器调用一个名为“contact”的方法。没有这条线,右键是不可见的。

            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Contact"
                                                                                  style:UIBarButtonItemStylePlain target:self action:@selector(contact:)];;
        

        【讨论】:

          【解决方案5】:

          在 Swift 2 中,你会这样做:

          let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
          self.navigationItem.rightBarButtonItem = rightButton
          

          (不是重大变化)在 Swift 4/5 中,它将是:

          let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: nil, action: nil)
          self.navigationItem.rightBarButtonItem = rightButton
          

          【讨论】:

            【解决方案6】:

            为什么不使用以下内容: (来自Draw custom Back button on iPhone Navigation Bar

            // Add left
            UINavigationItem *previousItem = [[UINavigationItem alloc] initWithTitle:@"Back title"];
            UINavigationItem *currentItem = [[UINavigationItem alloc] initWithTitle:@"Main Title"];
            [self.navigationController.navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil] animated:YES];
            
            // set the delegate to self
            [self.navigationController.navigationBar setDelegate:self];
            

            【讨论】:

              【解决方案7】:

              斯威夫特 3

                  let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(cancelPressed(_:)))
                  cancelBarButton.setTitleTextAttributes( [NSFontAttributeName : UIFont.cancelBarButtonFont(),
                                                                        NSForegroundColorAttributeName : UIColor.white], for: .normal)
                  self.navigationItem.leftBarButtonItem = cancelBarButton
              
              
                  func cancelPressed(_ sender: UIBarButtonItem ) {
                      self.dismiss(animated: true, completion: nil)
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-01-03
                相关资源
                最近更新 更多