【问题标题】:How to use info button on a UIBarButtonItem?如何在 UIBarButtonItem 上使用信息按钮?
【发布时间】:2016-01-10 21:54:37
【问题描述】:

如何使用UIBarButtonItem 上的信息灯?

我不想使用自定义图像,因为结果不是很好。

我想使用 Swift 在 UIBarButtonItem 上使用 Apple“信息”按钮。

【问题讨论】:

  • 在代码中还是通过 Interface Builder?
  • 代码,我认为Interface Builder不可能!谢谢
  • @Danny182 Interface Builder 有一种方法。请参阅我对类似问题的回答:stackoverflow.com/questions/39955353/…

标签: swift uibutton uibarbuttonitem


【解决方案1】:

没有直接的方法可以使用UIBarButtonItem APIs 创建这样的条形按钮。

您可以使用配置了.InfoLight UIButton 的自定义视图,如建议的here

// Create the info button
let infoButton = UIButton(type: .infoLight)

// You will need to configure the target action for the button itself, not the bar button itemr
infoButton.addTarget(self, action: #selector(getInfoAction), for: .touchUpInside)

// Create a bar button item using the info button as its custom view
let infoBarButtonItem = UIBarButtonItem(customView: infoButton)

// Use it as required
navigationItem.rightBarButtonItem = infoBarButtonItem

如果您需要更多帮助,请随时发表评论。

【讨论】:

    【解决方案2】:

    改编 mokagio 对 Obj-C 的出色回答:

    UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [infoButton addTarget:self action:@selector(getInfoAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* infoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    self.navigationItem.rightBarButtonItem = infoBarButtonItem;
    

    【讨论】:

      【解决方案3】:

      我会像这样轻松地重复使用它:

      class InfoBarButtonItem: UIBarButtonItem {
      
          init(_ type: UIButtonType = .infoLight, target: Any, action: Selector) {
              super.init()
              let button = UIButton(type: type)
              button.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
              self.customView = button
          }
      
          required init?(coder aDecoder: NSCoder) {
              fatalError("init(coder:) has not been implemented")
          }    
      }
      

      那么你可以这样使用:

      navigationItem.rightBarButtonItem =
                  InfoBarButtonItem(.infoLight, target: self, action: #selector(anAction(_:)))
      

      【讨论】:

      • 虽然这可行,但当扩展或辅助方法足够时,没有充分的理由为此子类化 UIBarButtonItem。
      • @KevinStewart 不过这只是个人喜好问题。
      【解决方案4】:

      您可以在 Interface Builder 中执行以下操作:

      1. 将 UIButton 对象拖到工具栏上

      2. 将按钮类型设置为“信息灯”

      【讨论】:

        【解决方案5】:

        Swift 4、iOS 12、Xcode 10

        另一种方法:

        1. 将条形按钮项从对象菜单拖到导航栏。

        2. 控制从 barButtonItem 拖动到您的视图控制器以创建一个插座。

        3. 在 viewDidLoad 中你可以指定 UIButton.customView 如下。

          let infoButton = UIButton(type: .infoLight)
          
          infoButton.addTarget(self, action: #selector(infoAction), for: .touchUpInside)
          
          infoBarButtonOutlet.customView = infoButton 
          
          //where infoBarButtonOutlet is the outlet you created in step 2.
          
          // Be sure to add @objc before func when you create your function.
          // for example: 
          @objc func infoAction () {
          
          }
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-04
          • 2012-03-11
          相关资源
          最近更新 更多