简体   繁体   English

如何在 swift 的导航栏中创建自定义后退按钮?

[英]How to create custom back button in navigationBar in swift?

public override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.backBarButtonItem = .init(image: "back_icon".image, style: .plain, target: nil, action: nil)
}

For this above code, I got my back_icon image with the default back image对于上面的代码,我得到了带有默认背景图像的 back_icon 图像

输出图像

Any idea to remove the default back button at the same time preserving swipe edge to pop viewController.在保留滑动边缘以弹出 viewController 的同时删除默认后退按钮的任何想法。

You should be able to change the back button image globally by using您应该能够通过使用全局更改后退按钮图像

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "arrow-back")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "arrow-back")

And if you only want to change it in some places:如果您只想在某些地方更改它:

navigationController?.navigationBar.backIndicatorImage = UIImage(named: "arrow-back")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "arrow-back")  

Removing an arrow at all would be as simple as完全删除箭头就像

navigationController?.navigationBar.backIndicatorImage = UIImage()
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage()

You can remove default back Button by following way:您可以通过以下方式删除默认返回按钮:

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

And manually add navigation items in left side using navigationitem.leftBarButtonItem.并使用navigationitem.leftBarButtonItem.

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM