简体   繁体   English

navigationController?.navigationBar.isUserInteractionEnabled无法正常工作

[英]navigationController?.navigationBar.isUserInteractionEnabled not working as expected

For the following viewController hierarchy, isUserInteractionEnabled doesn't appear to be working as expected. 对于以下viewController层次结构, isUserInteractionEnabled似乎未按预期工作。

NavigationController(ViewController A) --- pushes to ---> NavigationController(ViewController B) NavigationController(ViewController A)---推送至---> NavigationController(ViewController B)

In ViewController A's viewDidAppear method I set navigationController?.navigationBar.isUserInteractionEnabled to false and set it to true in ViewController B's viewDidAppear method. 在ViewController A的viewDidAppear方法中,我将navigationController?.navigationBar.isUserInteractionEnabled设置为false,并在ViewController B的viewDidAppear方法中将其设置为true。 However, upon popping ViewController B and returning to ViewController A, the navigation bar remains enabled for user interaction. 但是,在弹出ViewController B并返回到ViewController A时,导航栏将保持启用状态以用于用户交互。 Any thoughts as why this may be happening are greatly appreciated, thanks in advance! 非常感谢任何想法,为什么会发生这种情况,在此先感谢!

That seems to be a bug for which you could get around by doing that on the main thread: 这似乎是一个错误,您可以通过在主线程上执行此操作来解决:

override func viewDidAppear(_ animated: Bool) {
    //...        
    DispatchQueue.main.async {
        self.navigationController?.navigationBar.isUserInteractionEnabled = false
    }
}

But this still leaves a millisecond window where the navigationBar 's interaction is enabled. 但是,这仍然留下了一个毫秒窗口,在该窗口中启用了navigationBar的交互。
You have to be really quick. 您必须非常快。


However... 然而...

I wouldn't recommend what you're doing; 我不推荐你在做什么。 ie disabling the navigationBar . 即禁用navigationBar
You could lose the back ability, if it had one, because you're just disabling the navigationBar entirely. 如果具有back功能,则可能会失去它,因为您只是完全禁用了navigationBar

Suggestion: 建议:

Since every viewController in the navigation stack has it's own navigationItem , that contains it's own set of barButtonItems , I would recommend you keep references of the UIBarButtonItem and enable/disable them explicitly. 由于导航堆栈中的每个viewController都有其自己的navigationItem ,其中包含它自己的barButtonItems集, barButtonItems我建议您保留UIBarButtonItem引用并显式启用/禁用它们。

ie

@IBOutlet var myBarButtonItem: UIBarButtonItem!

override func viewDidAppear(_ animated: Bool) {
    //...
    myBarButtonItem.isEnabled = false
}

Furthermore, the state of this barButtonItem is handled in this viewController itself and you need not do things like self.navigationController?.navigationBar.isUserInteractionEnabled = true elsewhere. 此外,此barButtonItem的状态在此viewController本身中处理,您无需在其他地方执行诸如self.navigationController?.navigationBar.isUserInteractionEnabled = true类的事情。

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

相关问题 Swift3 WebView isUserInteractionEnabled无法正常工作 - Swift3 webview isUserInteractionEnabled not working swift isUserInteractionEnabled = false 在 UITapGestureRecognizer 内不起作用 - swift isUserInteractionEnabled = false not working inside UITapGestureRecognizer 在Swift中添加NavigationBar和NavigationController之后不应该调用shouldautorotate - Shouldautorotate not called after adding NavigationBar & NavigationController in Swift navigationBar:shouldPop无法正常运作 - navigationBar:shouldPop is not functioning as expected swift:button.isUserInteractionEnabled = false 滚动表格视图时停止工作 - swift: button.isUserInteractionEnabled = false stops working when scrolling tableview UISearchbar添加navigationController?.navigationBar,如何更改“取消”颜色 - UISearchbar add navigationController?.navigationBar,How to change “Cancel” color 将NavigationController的NavigationBar半透明属性设置为false会导致额外的填充 - Setting NavigationController's NavigationBar translucent property to false causes extra padding NavigationBar BACK按钮不起作用 - NavigationBar BACK button not working SWRevealController无法与NavigationController一起使用 - SWRevealController not working with NavigationController navigationController?.pushViewController 不工作 - navigationController?.pushViewController is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM