UIBarButtonItem remove Back Button Title - iOS 11

不羁的心 提交于 2019-12-22 08:20:48

问题


    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)

I use above to remove the backButtonTitle prior to iOS 11. But on iOS 11 that is not properly working. Arrow shifts bit downwards. How to fix this?

Edit: Other way by erasing Title can solve my problem but my concern is why that old way is not working anymore.


回答1:


The other way is to set UINavigationControllerDelegate for your navigationController and erase the title in its function.

class NoBackButtonTitleNavigationDelegate: UINavigationControllerDelegate {

    func navigationController(
        _ navigationController: UINavigationController,
        willShow viewController: UIViewController,
        animated: Bool
    ) {
        viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
    }
}



回答2:


Use this code,

var newBackButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(self.back))
navigationItem?.leftBarButtonItem = newBackButton 



回答3:


Create Navigation controller class as below. Assign this "CustomNavViewController" to UINavigationController in your StoryBoard

class CustomNavViewController: UINavigationController,UINavigationControllerDelegate 
{


override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
}

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


func navigationController(
    _ navigationController: UINavigationController,
    willShow viewController: UIViewController,
    animated: Bool
    ) {
    viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}

}

So, no need to do it in every viewControllers. At last remove below line from AppDelegate class if present,

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)



回答4:


Instead of moving title vertically you can move horizontally. Also in case the title is long you can make its color transparent. Works just fine me:

let barButtonItemAppearance = UIBarButtonItem.appearance()
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
    barButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)


来源:https://stackoverflow.com/questions/47248313/uibarbuttonitem-remove-back-button-title-ios-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!