我需要制作一个没有标题的后退按钮,如上图所示,所以我只需要“ <”酒吧按钮而不是“ <主页”酒吧按钮
要实现它,我在准备segue方法中使用了下面的代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// to remove title in back button in the segue destionation view controller
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
但是我必须将该代码放入许多视图控制器中。所以我认为我可以使用如下的UIViewController扩展使其更简单
extension UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for segue: UIStoryboardSegue, sender: Any?)
// to remove title in back button in the segue destionation view controller
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
因此,当我再次调用为segue做准备时,它将自动实现以下代码,而无需在每个视图控制器中再次写入该代码
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)