backBarButtonItem图像右对齐,想要居中

时间:2018-03-15 20:45:09

标签: ios swift uinavigationcontroller back-button

我正在尝试将backBarButtonItem设置为具有自定义图像:

self.navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back"), style: .plain, target: self, action: #selector(self.navigationController?.popViewController(animated:)))
self.navigationItem.backBarButtonItem?.tintColor = UIColor.red

我注意到当我为navigationController设置backBarButtonItem时,图像在backBarButtonItem的框架内正确对齐:

enter image description here

enter image description here

如何使图像居中,甚至左对齐?

1 个答案:

答案 0 :(得分:1)

而不是backBarButtonItem,您需要设置leftBarButtonItem

替换

self.navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back"), style: .plain, target: self, action: #selector(self.navigationController?.popViewController(animated:)))
self.navigationItem.backBarButtonItem?.tintColor = UIColor.red

通过此

self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back"), style: .plain, target: self, action: #selector(self.navigationController?.popViewController(animated:)))
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.red