我正在尝试将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的框架内正确对齐:
如何使图像居中,甚至左对齐?
答案 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