如何在iOS中的导航栏中添加标准信息按钮?

App*_*Dev 3 uinavigationbar uibarbuttonitem uinavigationitem ios swift

我想在导航栏中添加一个带有系统信息图标的右侧栏按钮.但我发现这UIBarButtonSystemItem并没有提供这种类型的按钮.另一方面,我尝试使用UIButton类型UIButtonType.infoLight,但我不允许将其分配给navigationItem.rightBarButtonItems.

有没有办法做到这一点?

Suh*_*til 14

这可以使用UIButton类型的对象来实现.infoLight

let infoButton = UIButton(type: .infoLight)
infoButton.addTarget(self, action: #selector(infoButtonTapped), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: infoButton)
self.navigationItem.rightBarButtonItem = barButton
Run Code Online (Sandbox Code Playgroud)

或者:

通过添加infoImage到您的Asset catalogbarButtonItem使用下面的代码创建

let infoButton = UIBarButtonItem(image: UIImage(named: "infoImage"), style: .Plain, target: self, action: #selector(ViewController.infoButtonTapped))
self.navigationItem.rightBarButtonItem = infoButton 
Run Code Online (Sandbox Code Playgroud)


VYT*_*VYT 5

我找到了一种使用 Interface Builder 向导航栏添加信息图标的更简单方法。

  1. 不要将 Bar Button Item 添加到导航栏
  2. 向 UIView 添加按钮
  3. 将按钮类型更改为信息灯
  4. 将按钮拖放到导航栏的右上角。这就是全部。Bar Button Item 将自动出现,添加的 info Button 将位于 Bar Button Item 下(然后,像往常一样对 Button 进行操作)