gpt4 book ai didi

ios - 在哪里设置 NavigationController.NavigationBar.isUserInteractionEnabled

转载 作者:行者123 更新时间:2023-11-29 00:12:00 25 4
gpt4 key购买 nike

我在将 navigationBar.isUserInteractionEnabled 设置为 false 时遇到问题。我在 viewDidLoad 和 viewDidLayoutSubviews 中设置它。但这个设置不起作用,因为即使我设置为 false,该值也是 true。我只需要在返回到上一个 View 时设置为 false(当单击 backButton 然后 navigationBar.isUserInteraction 应设置为 false 时)。我该怎么做?还有其他代表吗?或者我需要在调用 viewDidLoad 后设置计时器。 Here is an image to make it clear what I mean

最佳答案

如果您出于某些特定原因具体要禁用导航栏的用户交互,您可以通过调用来实现

swift :

self.navigationController?.navigationBar.isUserInteractionEnabled = false

objective-C :

[self.navigationController.navigationBar setUserInteractionEnabled:false];

编辑 2:

I need to set to false only when I back to previous View (when backButton is clicked and then navigationBar.isUserInteraction should be set to false)

您不能在 viewController 的 viewWillDisappear 中禁用导航栏用户交互,因为如果在调用 viewWillDisappear 时点击 backButton,ViewController 将从导航堆栈中删除。

因此,当您调用 self.navigationController 时,您的导航 Controller 为 nil。

最简单的解决方案,添加您自己的后退按钮并在实际弹出 VC 之前禁用导航栏的用户交互。

在你的第二个VC中

- (void)viewDidLoad {
[super viewDidLoad];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBack)];

self.navigationItem.leftBarButtonItem = backButton;
// Do any additional setup after loading the view from its nib.
}

-(void)handleBack {
[self.navigationController.navigationBar setUserInteractionEnabled:false];
[self.navigationController popViewControllerAnimated:true];
}

这样就可以了

编辑 3:

需要设置Navigation bar的User interaction可以通过禁用左右bar button items的user interaction达到同样的效果

    for item in self.navigationItem.leftBarButtonItems! {
item.isEnabled = false
}

for item in self.navigationItem.rightBarButtonItems! {
item.isEnabled = false
}

您可以根据您的逻辑在 viewDidLoad、viewWillAppear 或任何适合您的代码的地方启用和禁用它们。

objective-C

for(UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
[item setEnabled:false];
}

for(UIBarButtonItem *item in self.navigationItem. rightBarButtonItems) {
[item setEnabled:false];
}

关于ios - 在哪里设置 NavigationController.NavigationBar.isUserInteractionEnabled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com