在我的许多项目中,我使用了一个自定义的后退按钮,它只是一个没有文本的图标。我做了一些实验以找到最好的方法并提出了以下方法(我并不是说它是最好的方法或唯一的方法,但它对我有用并且稳定)。
我的想法是实现一个 Root ViewController 来重新设置标准后退按钮的样式。事实证明这根本不容易。在摆弄了大约 1 天的不同选项后,我终于找到了一个可行的解决方案。
诀窍是要有一个足够大的透明背景(比实际的背面 V 形更大,否则它会被调整为更小的尺寸),然后为 V 形使用自定义图像。
它有效,但正如您在下面的测量结果中看到的那样,它有一个小缺点:左侧有额外的 14 点间隙。如果您想在右侧匹配按钮,则需要将其补偿 14 个点(是的,它的点在视网膜上是 28 个像素...)

这是我的代码:
//this vc implements a custom back button, you can make this a root controller
//from which you inherit all your view controllers. But for simplicity reasons for this explanation
//I skipped this
class VCRoot: UIViewController {
override func viewDidLoad() {
//call supers
super.viewDidLoad()
//create custom back item
let backItem = UIBarButtonItem()
//as image set the back chevron icon its a 22x22 points (so 44x44 in retina)
backItem.image=PaintCode.imageOfBarBtnBack
let b = PaintCode.imageOfBarBtnBackgroundEmpty
//as background I use a 1 = 33 points (so 2x66 retina) fully transparant image
backItem.setBackButtonBackgroundImage(b, forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
//set the newly created barbuttonitme as the back button
navigationItem.backBarButtonItem=backItem
}
}
//this vc puts a "forwards" button in the navbar on the right with a matching arrow
//The image of this matching forward arrow is the correct size (width!) so that its the same
//distance from the edge of the screen as the back button
class VC2: UIViewController {
//outlet to the barbutton item from IB
@IBOutlet weak var barbtn: UIBarButtonItem!
override func viewDidLoad() {
//call supers
super.viewDidLoad()
//the forward chevron in the image is shifted 14 points (so 28 retina) to the left
//so it has same distance from edge => its (22+14) x 22 = 36 x 22 points (72 x 44 retina)
barbtn.image=PaintCode.imageOfBarBtnForward
}
}
您也可以在这里参考我的博客:
http ://www.hixfield.net/blog/2015/05/adding-a-custom-back-button-the-standard-way/