iOS 如何将标准信息按钮添加到导航栏?
How to add a standard info button to a navigation bar in iOS?
我想在导航栏中添加一个带有系统信息图标的右栏按钮。但是我看到 UIBarButtonSystemItem
没有提供这种类型的按钮。另一方面,所以我尝试使用 UIButtonType.infoLight
类型的 UIButton
,但不允许我将其分配给 navigationItem.rightBarButtonItems
。
有什么办法吗?
您可以使用自定义视图初始化 UIBarButtonItem
:
let button = UIButton(type: .infoLight)
let barButton = UIBarButtonItem(customView: button)
将 infoButton 图像添加到您的资产中,并通过提供名称将其设置为 barButton。
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"infoImage.png"] style:UIBarButtonItemStyleDone target:self action:@selector(barButtonAction)];
self.navigationItem.rightBarButtonItem = item;
在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;
这可以使用 UIButton
类型 .infoLight
的对象来实现
let infoButton = UIButton(type: .infoLight)
infoButton.addTarget(self, action: #selector(infoButtonTapped), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: infoButton)
self.navigationItem.rightBarButtonItem = barButton
或者:
通过将 infoImage
添加到您的 Asset catalog
并使用以下代码创建 barButtonItem
let infoButton = UIBarButtonItem(image: UIImage(named: "infoImage"), style: .Plain, target: self, action: #selector(ViewController.infoButtonTapped))
self.navigationItem.rightBarButtonItem = infoButton
我找到了一种使用 Interface Builder 将信息图标添加到导航栏的更简单的方法。
- 不要将栏按钮项目添加到导航栏
- 向 UIView 添加按钮
- 将按钮类型更改为信息灯
- 将按钮拖放到导航栏的右上角。这就是全部。 Bar Button Item会自动出现,添加的信息Button会在Bar Button Item下面
(然后,像往常一样为按钮做一个动作)
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() {}
我想在导航栏中添加一个带有系统信息图标的右栏按钮。但是我看到 UIBarButtonSystemItem
没有提供这种类型的按钮。另一方面,所以我尝试使用 UIButtonType.infoLight
类型的 UIButton
,但不允许我将其分配给 navigationItem.rightBarButtonItems
。
有什么办法吗?
您可以使用自定义视图初始化 UIBarButtonItem
:
let button = UIButton(type: .infoLight)
let barButton = UIBarButtonItem(customView: button)
将 infoButton 图像添加到您的资产中,并通过提供名称将其设置为 barButton。
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"infoImage.png"] style:UIBarButtonItemStyleDone target:self action:@selector(barButtonAction)];
self.navigationItem.rightBarButtonItem = item;
在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;
这可以使用 UIButton
类型 .infoLight
let infoButton = UIButton(type: .infoLight)
infoButton.addTarget(self, action: #selector(infoButtonTapped), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: infoButton)
self.navigationItem.rightBarButtonItem = barButton
或者:
通过将 infoImage
添加到您的 Asset catalog
并使用以下代码创建 barButtonItem
let infoButton = UIBarButtonItem(image: UIImage(named: "infoImage"), style: .Plain, target: self, action: #selector(ViewController.infoButtonTapped))
self.navigationItem.rightBarButtonItem = infoButton
我找到了一种使用 Interface Builder 将信息图标添加到导航栏的更简单的方法。
- 不要将栏按钮项目添加到导航栏
- 向 UIView 添加按钮
- 将按钮类型更改为信息灯
- 将按钮拖放到导航栏的右上角。这就是全部。 Bar Button Item会自动出现,添加的信息Button会在Bar Button Item下面 (然后,像往常一样为按钮做一个动作)
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() {}