如何更改UIBarButtonItem在UINavigationBar中的位置?我希望我的按钮比正常位置高出5px。
发布于 2020-12-17 11:24:14
如果您只是想像我一样调整自定义后退按钮的位置,我使用了一个向UIImage本身添加插件的解决方案来实现这一点。
我使用的解决方案来自:https://stackoverflow.com/a/31240900/1241783
添加此扩展函数
import UIKit
extension UIImage {
func imageWithInsets(insets: UIEdgeInsets) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(
CGSize(width: self.size.width + insets.left + insets.right,
height: self.size.height + insets.top + insets.bottom), false, self.scale)
let _ = UIGraphicsGetCurrentContext()
let origin = CGPoint(x: insets.left, y: insets.top)
self.draw(at: origin)
let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageWithInsets
}
}然后在自定义后退按钮时就像这样使用它
let backIcon = UIImage(named: "back_btn_icon")!.imageWithInsets(insets: UIEdgeInsets(top: 0, left: 0, bottom: 5, right: 0))
navigationController?.navigationBar.backIndicatorImage = backIcon
navigationController?.navigationBar.backIndicatorTransitionMaskImage = backIcon根据需要调整插图
https://stackoverflow.com/questions/5761183
复制相似问题