iOS UINavigationBar button remains faded after segue back

三世轮回 提交于 2019-11-26 20:48:59

This is a bug in iOS 11.2 and happens because the UIBarButtonItem stays highlighted after navigation and does not return to its normal state after the other view controller pops.

To avoid this behavior, either

  1. use a UIBarButtonItem with a UIButton as a custom view

  2. disable and re-enable the bar button item in viewWillDisappear(_:) (although this causes the button to appear immediately, use matt's solution to avoid this):

    barButtonItem.isEnabled = false
    barButtonItem.isEnabled = true
    

What I do is work around this bug, in the view controller's viewWillAppear, as follows:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.tintAdjustmentMode = .normal
    self.navigationController?.navigationBar.tintAdjustmentMode = .automatic
}

That seems to wake up the button without visual artifacts.

Another work around is to implement the fix on the parent navigationController - so that each of its child viewController's gets the fix as follows

NOTE: This requires the receiving class to be setup as the UINavigationController delegate

Swift

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    if #available(iOS 11.2, *) {
        navigationBar.tintAdjustmentMode = .normal
        navigationBar.tintAdjustmentMode = .automatic
    }
}

Objective-C

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {    
    if (@available(iOS 11.2, *)) {
        self.navigationBar.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
        self.navigationBar.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
    }
}

I solved it like this:

override func viewWillDisappear(_ animated: Bool) {
    navigationController?.navigationBar.tintAdjustmentMode = .normal
    navigationController?.navigationBar.tintAdjustmentMode = .automatic
}

so it will restore the color before the other view appear

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