[英]How can I hide the back arrow in the navigation bar's back button?
I have a navigation controller for which I'd like to override the default back arrow image and text.我有一个导航 controller,我想为其覆盖默认的后退箭头图像和文本。 Basically, instead of the
< Back
, I'd like to hide the little <
arrow, and just show the Back
text.基本上,我想隐藏小
<
箭头,而不是< Back
,只显示Back
文本。 I've been able to replace the back arrow with another image, so I figured i'd just try to replace it with an image and then set the image to match the color of the background.我已经能够用另一个图像替换后退箭头,所以我想我只是尝试用图像替换它,然后将图像设置为与背景颜色相匹配。 How can I accomplish this?
我怎样才能做到这一点?
In AppDelegate.swift I have this:在 AppDelegate.swift 我有这个:
extension UINavigationItem{
override open func awakeFromNib() {
super.awakeFromNib()
let backItem = UIBarButtonItem()
backItem.title = ""
backItem.image = UIImage(named: "icons8_burger")
self.backBarButtonItem = backItem
}
}
In the viewDidLoad() for the controller that we are sent to when the button is clicked, I have this:在单击按钮时我们发送到的 controller 的 viewDidLoad() 中,我有这个:
override func viewDidLoad() {
super.viewDidLoad()
let burger = UIImage(named: "icons8_burger")
navigationController?.navigationBar.backIndicatorImage = burger
navigationController?.navigationBar.backIndicatorTransitionMaskImage = burger
}
So, any ideas how I can accomplish this?那么,有什么想法可以实现吗? I haven't found what I'm looking for thus far.
到目前为止,我还没有找到我要找的东西。
Any advice appreciated!任何建议表示赞赏!
If you do , 如果这样做,
navigationController?.navigationBar.backIndicatorImage = nil
navigationController?.navigationBar.backIndicatorTransitionMaskImage = nil
or 要么
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "")
Then by default iOS use <
as default image. 然后默认情况下,iOS使用
<
作为默认图像。
To remove this image you need to add blank transparent image. 要删除此图像,您需要添加空白的透明图像。 I used sample from here and google image reference is here
我从这里使用示例,并且Google图像参考在这里
Now reduce the image pixel size to (1,1)
and add in your assets and use below code. 现在将图像像素大小减小到
(1,1)
并添加您的资产并使用以下代码。
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Blank")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Blank")
Here Blank is the same image name in my asset. 在这里, 空白是我资产中的相同图像名称。
Use an empty image object like below, 使用一个空的图像对象,如下所示,
let mask = UIImage()
navigationController?.navigationBar.backIndicatorImage = mask
navigationController?.navigationBar.backIndicatorTransitionMaskImage = mask
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
If you want to use the appearance - both backIndicator*
vars are read-only.如果您想使用外观 - 两个
backIndicator*
变量都是只读的。
Try the next code-only approach.尝试下一个纯代码方法。 Tested on iOS 15.2
测试于 iOS 15.2
private func setupNavBar() {
let navBarAppearance = UINavigationBarAppearance()
let blankImage = UIImage.withColor(UIColor.white.withAlphaComponent(0))
navBarAppearance.setBackIndicatorImage(blankImage, transitionMaskImage: blankImage)
}
extension UIImage {
static func withColor(_ color: UIColor) -> UIImage {
let onePx = pixelsToPoints(1)
let rect = CGRect(x: 0, y: 0, width: onePx, height: onePx)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let context = UIGraphicsGetCurrentContext()
context!.setShouldAntialias(false)
context!.setFillColor(color.cgColor)
context!.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
static func pixelsToPoints(_ pixels: CGFloat) -> CGFloat {
return pixels / UIScreen.main.scale
}
}
将emply图片应用于navigationController
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.