backBarButtonItem 图像右对齐,希望居中
backBarButtonItem image right aligned, want centered
我正在尝试将 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 的框架内右对齐:
如何使图像居中,甚至左对齐?
您需要设置 leftBarButtonItem
而不是 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
由此
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back"), style: .plain, target: self, action: #selector(self.navigationController?.popViewController(animated:)))
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.red
我正在尝试将 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 的框架内右对齐:
如何使图像居中,甚至左对齐?
您需要设置 leftBarButtonItem
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
由此
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back"), style: .plain, target: self, action: #selector(self.navigationController?.popViewController(animated:)))
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.red