xcode setNavigationBarHidden:NO 在 removeFromSuperview 后不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6007347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:01:49  来源:igfitidea点击:

setNavigationBarHidden:NO Not Working after removeFromSuperview

iphoneobjective-cxcodeios

提问by user754314

I am adding a UIWebView subview on top of my Detail View Controller which has a navigation bar. I would like to hide the navigation bar when in the WebView subview and reveal it again when removing from the superview, so that there is more screen room while viewing the WebView.

我在我的细节视图控制器顶部添加了一个 UIWebView 子视图,它有一个导航栏。我想在 WebView 子视图中隐藏导航栏,并在从超级视图中删除时再次显示它,以便在查看 WebView 时有更多的屏幕空间。

The problem with my code is that the navigation bar is successfully hidden after adding the subview, but it doesn't work when trying to reveal the navigation bar again when the subview is removed.

我的代码的问题是添加子视图后导航栏成功隐藏,但是当删除子视图时尝试再次显示导航栏时它不起作用。

Any help would be much appreciated. Thank you.

任何帮助将非常感激。谢谢你。

This is my code:

这是我的代码:

// In InstrumentsDetailViewController.m

- (IBAction)edu1Link:(id)sender {

    _webViewController = [[WebViewController alloc]
                          initWithNibName:@"WebViewController" bundle:nil];

    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft
                           forView:self.view cache:YES];

    [self.view addSubview:_webViewController.view];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    [UIView commitAnimations];
}


// In WebViewController.m

- (IBAction) doneButton:(id)sender {

    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
                           forView:self.view.superview cache:YES];

    [self.view removeFromSuperview];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

    [UIView commitAnimations];
}

回答by LombaX

For what I see in the code: - you are adding a subview to your InstrumentsDetailViewController instance main view. - The subview is the main view of a WebViewController instance.

对于我在代码中看到的内容: - 您正在向 InstrumentsDetailViewController 实例主视图添加一个子视图。- 子视图是 WebViewController 实例的主视图。

But...the WebViewController is never pushed or popped to your navigation stack. For this reason, your WebViewController instance has NO reference to your navigationController, and calling [self.navigationController] will send your setNavigationBarHidden:NO animated:YES message to...nil

但是... WebViewController 永远不会被推送或弹出到您的导航堆栈。因此,您的 WebViewController 实例没有对您的 navigationController 的引用,并且调用 [self.navigationController] 会将您的 setNavigationBarHidden:NO animation:YES 消息发送到...nil

You can: - push/pop your WebviewController instance to the navigation stack, so it will have a reference to the navigationController. or - add your WebViewController instance as a CHILD of the InstrumentsDetailViewController and then call

您可以: - 将您的 WebviewController 实例推送/弹出到导航堆栈,因此它将引用导航控制器。或 - 添加您的 WebViewController 实例作为 InstrumentsDetailViewController 的子项,然后调用

[[[self parentViewController] navigationController] setNavigationBarHidden:NO animated:YES];

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

search for addChildViewController:

搜索 addChildViewController: