[英]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. 您必须非常快。
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.