ios 如何向导航栏添加/使用默认图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32558014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add / use default icons to navigation bar
提问by pbaranski
I want to use some of default iOS iconsi.e.
我想使用一些默认的iOS 图标,即
in navigation bar.
Basically I don't know how to call image of that item (directly from native library - I know how to download it and place as custom image:)):
在导航栏中。
基本上我不知道如何调用该项目的图像(直接从本机库 - 我知道如何下载它并放置为自定义图像:)):
var myButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
myButton.addTarget(self, action: "reload", forControlEvents: UIControlEvents.TouchUpInside)
myButton.setImage(???, forState: <#UIControlState#>)
回答by Dharmesh Kheni
You can use UIBarButtonSystemItem
this way:
你可以这样使用UIBarButtonSystemItem
:
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "someAction")
navigationItem.leftBarButtonItem = button
Result for leftBarButtonItem
:
结果为leftBarButtonItem
:
If you want to set it at right side you can use this code:
如果您想将其设置在右侧,您可以使用以下代码:
navigationItem.rightBarButtonItem = button
Result for rightBarButtonItem
:
结果为rightBarButtonItem
:
回答by Nitin Nain
Swift:These are the most commonly used options:
Swift:这些是最常用的选项:
To Use custom image with original colour:
要使用具有原始颜色的自定义图像:
let customImageBarBtn1 = UIBarButtonItem(
UIImage(named: "someImage.png").withRenderingMode(.alwaysOriginal),
style: .plain, target: self, action: #selector(handleClick))
To Use custom image with tint colour:
要使用带有色调的自定义图像:
let customImageBarBtn2 = UIBarButtonItem(
UIImage(named: "someImage.png").withRenderingMode(.alwaysTemplate),
style: .plain, target: self, action: #selector(handleClick))
Or use system provided buttons:
或者使用系统提供的按钮:
let systemBarBtn = UIBarButtonItem(
barButtonSystemItem: .search,
target: self, action: #selector(handleClick))
Then add any one of these buttons to the navigationItem:
然后将这些按钮中的任何一个添加到导航项中:
navigationItem.leftBarButtonItems = [customImageBarBtn1, customImageBarBtn2]
navigationItem.rightBarButtonItems = [systemBarBtn]
// OR you can use this if there's only one item.
navigationItem.rightBarButtonItem = systemBarBtn
For custom Images:As a starting size, 22ptx22pt
images work well for the default iPhone Navigation Bar size.
对于自定义图像:作为起始尺寸,22ptx22pt
图像适用于默认的 iPhone 导航栏尺寸。
回答by Sanjay Mishra
In swift 4.3
在快速 4.3
let btnRefresh = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.refresh, target: self, action: #selector(targeted function to invoke))
//If you want icon in left side
navigationItem.leftBarButtonItem = btnRefresh
//If you want icon in right side
navigationItem.rightBarButtonItem = btnRefresh