How to add a standard info button to a navigation

2019-07-13 17:29发布

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. 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.

Is there any way to do this?

6条回答
Viruses.
2楼-- · 2019-07-13 18:05

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() {}
查看更多
冷血范
3楼-- · 2019-07-13 18:05

Add the infoButton image into your assets and set as barButton by providing the name .

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
查看更多
闹够了就滚
4楼-- · 2019-07-13 18:09

this can be achieved using UIButton object of type .infoLight

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

let infoButton = UIBarButtonItem(image: UIImage(named: "infoImage"), style: .Plain, target: self, action: #selector(ViewController.infoButtonTapped))
self.navigationItem.rightBarButtonItem = infoButton 
查看更多
聊天终结者
5楼-- · 2019-07-13 18:09
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;
查看更多
欢心
6楼-- · 2019-07-13 18:15

You can initialize a UIBarButtonItem with a custom view:

let button = UIButton(type: .infoLight)
let barButton = UIBarButtonItem(customView: button)
查看更多
疯言疯语
7楼-- · 2019-07-13 18:19

I have found an easier way to add info icon to a navigation bar using Interface Builder.

  1. Don't add Bar Button Item to the navigation bar
  2. Add a Button to the UIView
  3. Change a button type to 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)
查看更多
登录 后发表回答