Adjust position of custom Navigation Back Button

一个人想着一个人 提交于 2019-12-24 18:48:31

问题


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)
        }
}

This is what it looks like now:

As you can see I need to move the back button down a little, any help would be much appreciated.


回答1:


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



来源:https://stackoverflow.com/questions/56857833/adjust-position-of-custom-navigation-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!