如何在导航栏中添加/使用默认图标
我想使用一些默认的iOS图标,即
在导航栏中。
基本上我不知道如何调用该项目的图像(直接从本机库 – 我知道如何下载它并放置为自定义图像:)):
var myButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton myButton.addTarget(self, action: "reload", forControlEvents: UIControlEvents.TouchUpInside) myButton.setImage(???, forState: )
您可以这样使用UIBarButtonSystemItem
:
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "someAction") navigationItem.leftBarButtonItem = button
leftBarButtonItem
结果:
如果要将其设置在右侧,可以使用以下代码:
navigationItem.rightBarButtonItem = button
rightBarButtonItem
结果:
Swift 3:这些是最常用的选项:
要使用原始颜色的自定义图像:
let customImageBarButton1 = UIBarButtonItem(UIImage(named: "someImage.png").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleClick))
要使用带有淡色的自定义图像:
let customImageBarButton2 = UIBarButtonItem(UIImage(named: "someImage.png").withRenderingMode(.alwaysTemplate), style: .plain, target: self, action: #selector(handleClick))
或使用系统提供的按钮:
let systemBarButton = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(handleClick))
然后将以下任何一个按钮添加到navigationItem:
navigationItem.leftBarButtonItems = [customImageBarButton1, customImageBarButton2] navigationItem.rightBarButtonItem = [systemBarButton] // OR you can use `navigationItem.rightBarButtonItems = systemBarButton` for one item
对于自定义图像:作为起始大小, 22ptx22pt
图像适用于默认的iPhone导航栏大小。