问题描述
我使用自定义图像作为导航控制器的后退按钮,但问题是图像没有与标题和右侧按钮项正确对齐.我一直试图将后退按钮向下移动几个像素,但没有成功.
I am using a custom image as the back button of my Navigation Controller but the problem is that the image is not aligned correctly with the title and the right button item. I've been trying to move the back button down a few pixels with no success.
extension UINavigationController {
func addBackButton() {
let imgBack = UIImage(named: "ic_back")
navigationBar.backIndicatorImage = imgBack
navigationBar.backIndicatorTransitionMaskImage = imgBack
navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "",
style: .plain,
target: self,
action: nil)
}
}
这是现在的样子:
如您所见,我需要将后退按钮向下移动一点,我们将不胜感激.
As you can see I need to move the back button down a little, any help would be much appreciated.
推荐答案
这是我的自定义后退按钮代码.我在 viewDidLoad() 上添加了下图.也许你需要用你的图像测试如何正确调整它的大小.您可以删除带有标签的部分.
This is my code for a custom back button. I added on viewDidLoad() with the image below. Maybe you need to test with your image how to size it correctly. And you can remove the part with the label.
let backButtonView = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 44))
let imageView = UIImageView(image: UIImage(named: "back-arrow"))
imageView.frame = CGRect(x: -5, y: 11, width: 12, height: 22)
imageView.image = imageView.image!.withRenderingMode(.alwaysTemplate)
imageView.tintColor = .blue
let label = UILabel(frame: CGRect(x: 10, y: 0, width: 40, height: 44))
label.textColor = .blue
label.text = "Back"
backButtonView.addSubview(imageView)
backButtonView.addSubview(label)
backButtonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backButtonClicked)))
let barButton = UIBarButtonItem(customView: backButtonView)
navigationItem.leftBarButtonItem = barButton
这篇关于调整自定义导航返回按钮的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!