简体   繁体   English

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

[英]How to add a standard info button to a navigation bar in iOS?

I'd like to add a right bar button with the system's info icon to a navigation bar. 我想在导航栏中添加一个带有系统信息图标的右侧栏按钮。 But I see that UIBarButtonSystemItem does not provide such type of button. 但我发现UIBarButtonSystemItem不提供这种类型的按钮。 On the other hand, so I tried with a UIButton of type UIButtonType.infoLight , but I am not allowed to assign it to navigationItem.rightBarButtonItems . 另一方面,我尝试使用UIButton类型的UIButtonType.infoLight ,但我不允许将它分配给navigationItem.rightBarButtonItems

Is there any way to do this? 有没有办法做到这一点?

this can be achieved using UIButton object of type .infoLight 这可以使用.infoLight类型的UIButton对象来实现

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

alternatively: 或者:

by adding infoImage to your Asset catalog and create barButtonItem using below code 通过将infoImage添加到您的Asset catalog并使用下面的代码创建barButtonItem

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

You can initialize a UIBarButtonItem with a custom view: 您可以使用自定义视图初始化UIBarButtonItem

let button = UIButton(type: .infoLight)
let barButton = UIBarButtonItem(customView: button)

Swift 5.0 Swift 5.0

override func viewDidLoad() {
    super.viewDidLoad()

    let infoButton = UIButton(type: .infoLight)
    infoButton.addTarget(self, action: #selector(infoButtonTapped), for: .touchUpInside)
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: infoButton)
}

@objc func infoButtonTapped() {}

I have found an easier way to add info icon to a navigation bar using Interface Builder. 我找到了一种使用Interface Builder将信息图标添加到导航栏的更简单方法。

  1. Don't add Bar Button Item to the navigation bar 不要将栏按钮项添加到导航栏
  2. Add a Button to the UIView 向UIView添加一个按钮
  3. Change a button type to Info Light 将按钮类型更改为Info Light
  4. Drag and drop the button to the right corner of the navigation bar. 将按钮拖放到导航栏的右上角。 It is all. 这是全部。 Bar Button Item will appear automatically and the added info Button will be under the Bar Button Item (Then, make an action as usual for the Button) 条形按钮项目将自动出现,添加的信息按钮将位于条形按钮项目下(然后,像往常一样对按钮执行操作)

Add the infoButton image into your assets and set as barButton by providing the name . 将infoButton图像添加到资源中,并通过提供名称将其设置为barButton。

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"infoImage.png"] style:UIBarButtonItemStyleDone target:self action:@selector(barButtonAction)];

    self.navigationItem.rightBarButtonItem = item;

In Swift 在斯威夫特

  let item = UIBarButtonItem(image: UIImage(named: "infoImage.png"), style: .Plain, target: self, action: #selector(barButtonAction))

        self.navigationItem.rightBarButtonItem = item
UIButton *doneBtn = [[UIButton alloc] initWithFrame...];
[doneBtn setTitle:@"Submit" forState:UIControlStateNormal];
doneBtn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[doneBtn addTarget:self action:@selector(doSubmit:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
self.navigationItem.rightBarButtonItem = rightButton;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM