我想创建一个UIBarButtonItem来表示应用程序的设置(齿轮)。目前我只能找到创建UIBarButtonItem(Interface Builder> Attributes Inspector>标识符)的选项,例如“添加”(+),“编辑”,“完成”,“取消”等
我找不到创建设置(齿轮)图标的选项。有没有办法在界面构建器中或通过代码执行此操作?
或者我是否必须创建图像然后创建图像?
答案 0 :(得分:63)
Unicode有几个值得注意的例子,你可以简单地复制并粘贴到Xcode中的字符串声明中,或者使用标准的Unicode String Escape(\ uxxxx),iOS实际上非常流利(我知道一些字符串)是相当丑陋的,但这是你的Unicode'):
Unicode字符'没有中心的GEAR'(U + 26ED):http://www.fileformat.info/info/unicode/char/26ed/index.htm
Unicode字符'GEAR'(U + 2699):http://www.fileformat.info/info/unicode/char/2699/index.htm
或者准备一个图像并相应地设置UIBarButtonItem的customView属性。
答案 1 :(得分:15)
撰写 CodaFi 和 user1046037 答案:
使用unicode字符作为标题创建UIBarButtonItem。
您必须使用标题(UIBarButtonItem
)而不是系统项initWithTitle:
初始化initWithBarButtonSystemItem:
。
您可以使用字符串设置自定义标题(例如unicode字符)。
您可以调整标题大小。
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"\u2699" style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)];
UIFont *customFont = [UIFont fontWithName:@"Helvetica" size:24.0];
NSDictionary *fontDictionary = @{NSFontAttributeName : customFont};
[settingsButton setTitleTextAttributes:fontDictionary forState:UIControlStateNormal];
答案 2 :(得分:6)
要使用Swift获取齿轮图标,请在viewDidLoad()
中执行以下操作,假设您已将视图中的按钮连接到控制器。 (例如:@IBOutlet var settingsButton: UIBarButtonItem!
)
self.settingsButton.title = NSString(string: "\u{2699}") as String
if let font = UIFont(name: "Helvetica", size: 18.0) {
self.settingsButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
答案 3 :(得分:6)
这适用于Swift 3和iOS 10 ......
let settingsButton = UIBarButtonItem(title: NSString(string: "\u{2699}\u{0000FE0E}") as String, style: .plain, target: self, action: #selector(settings))
let font = UIFont.systemFont(ofSize: 28) // adjust the size as required
let attributes = [NSFontAttributeName : font]
settingsButton.setTitleTextAttributes(attributes, for: .normal)
在接受的答案中查看@ hazernut的评论。如果不向字符串添加\u{0000FE0E}
,它就会显示为表情符号,并且不受任何外观设置的影响。添加该字符串可修复该问题。