Code for no text at back button at navigation bar

时间:2015-05-04 19:54:56

标签: swift

I have the following code for letting the back button in my navigation bar stay with no text:

self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
let backItem = UIBarButtonItem(title: "", style: .Bordered, target: nil, action: nil)
    navigationItem.backBarButtonItem = backItem

This worked until i updated xCode. Now I get this error message:

'Bordered' was deprecated in iOS version 8.0: Use UIBarButtonItemStylePlain when
minimum deployment target is iOS 7.0 or later.

thanks for your help

2 个答案:

答案 0 :(得分:4)

Just set the style to plain. Like this

let backItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem

答案 1 :(得分:1)

小心点。除iPhone 6 / 6s Plus外,此技术适用于所有设备!

这个问题的共同答案是:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)

Apple的文档说使用nil代替标题:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: .Plain, target: nil, action: nil)

这两种技术似乎都适用于所有模拟器和实际设备,除了" iPhone plus"。要纠正,而不是创建自己的Rects或类似的聪明,你可以为标题分配一个空格,如下:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)

我不喜欢这个答案,但确实有效。答案真的应该是将标题设置为nil,就像Apple文档一样。我无法相信这么大的公司无法对此进行测试!