自定义应用程序中所有外观的后退按钮

时间:2018-05-23 15:27:56

标签: ios swift uinavigationcontroller

我想通过将文本设置为" Back"来更改应用中所有后退按钮的外观。并删除箭头(即使只是删除箭头就可以了)。我试图找到一种方法在应用程序中为所有视图控制器进行全局操作,同时保留功能(我不想创建UIBarButtonItem的新实例并且必须设置selector)。

我创建了一个自定义UINavigationController并尝试使用navigationItem.backBarButtonItem?.title = "Back"设置后面的按钮标题,但它没有用。有任何建议如何正确地做到这一点?

2 个答案:

答案 0 :(得分:2)

然后创建CustomNavigationController override open func pushViewController(_ viewController: UIViewController, animated: Bool)执行controlClearBackTitle方法

然后使用CustomNavigationController在您的应用中修复所有内容

enter image description here

import UIKit

class CustomNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
        controlClearBackTitle()
        super.pushViewController(viewController, animated: animated)
    }

    override open func show(_ vc: UIViewController, sender: Any?) {
        controlClearBackTitle()
        super.show(vc, sender: sender)
    }

    func controlClearBackTitle() {

        self.navigationBar.backIndicatorImage = UIImage()
        self.navigationBar.backIndicatorTransitionMaskImage = UIImage()
        topViewController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "AnyTitle", style: .plain, target: nil, action: nil)

    }
}

答案 1 :(得分:1)

我认为这样的事情应该这样做。尝试将其放入自定义NavigationController中。

let backImage = UIImage(named: "backImage")
self.navigationController?.navigationBar.backIndicatorImage = backImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage

修改 对不起完全忘记了标题。这应该这样做:

self.navigationController?.navigationBar.backItem?.title = "New title"