iOS UINavigationBar button remains faded after segue back

问题内容:

In my app I have multiple view controllers, and most have a right-hand-side UIBarButtonItem with direct “show” segue actions attached.

Having segued to another view and then pressed the '< Back' button, the original button item remains faded out, although still otherwise usable.

This only appears to happen under iOS 11.2.

I can’t see any setting that could be doing this, and in at least one of the cases where this happens there’s no specific segue unwinding nor viewDidAppear handling. I’d post some code, but AFAICS it’s all just default UINavigationBar behaviour.

问题评论:

答案:

答案1:

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(_:):

    barButtonItem.isEnabled = false
    barButtonItem.isEnabled = true
    

答案评论:

    
Thanks for the reply. Might it also be possible to reset the button’s state in the viewDidAppear() method?
– Alnitak
2 hours ago
    
@Alnitak I added a second option to my answer which addresses the issue in a different way.
1  
thanks – either seems to work, I’m unsure which I find more visually appealing. If I do it in viewWillDisappear(_:) then the button is immediately visible when the view pops. If I do it in viewDidAppear(_:) then it animates back into visibility.
– Alnitak
2 hours ago
    
p.s. if you know of any links to other reports of this behaviour that would be very useful
– Alnitak
2 hours ago

原文地址:

https://stackoverflow.com/questions/47754472/ios-uinavigationbar-button-remains-faded-after-segue-back

添加评论

友情链接:蝴蝶教程