Navigation bar not showing

吃可爱长大的小学妹 提交于 2019-12-10 13:44:00

问题


Here I have 2 views:

  • WelcomeVC
  • WebViewVC

First AppDelegate calls WelcomeVC through this code below:

- (void)presentWelcomeViewController 
    WelcomeViewController *welcomeViewController = [[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil];

    welcomeViewController.title = @"Welcome to My App";

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:welcomeViewController];
    navController.navigationBarHidden = YES;

    self.viewController = navController;
    self.window.rootViewController = self.viewController;
}

So in WelcomeVC, navigation bar is not shown with navController.navigationBarHidden = YES;. In WelcomeVC, there is a button to call WebViewVC as detailed below:

- (IBAction)termsPressed:(id)sender {
    WebViewController *webViewController = [[WebViewController alloc] initWithNibName:nil bundle:nil];
    NSLog(@"Terms of Use");
    webViewController.urlPassed = @"http://.../term.html";
    webViewController.title = NSLocalizedString(@"Terms of Use", nil);
    [webViewController.navigationController setNavigationBarHidden:NO animated:NO];
    [self.navigationController presentViewController:webViewController animated:YES completion:^{}];    
}

When this button is pressed, I want it to call WebViewVC with navigation bar presented as I do [webViewController.navigationController setNavigationBarHidden:NO animated:NO]; but what I found is WebViewVC is presented without navigation bar still. I also include viewDidLoad in WebViewVC as shown below:

- (void)viewDidLoad {
    [super viewDidLoad];

    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    if(screenSize.height == 480)
    {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    }
    else if(screenSize.height == 568)
    {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    }

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlPassed]]];
    [self.view addSubview:webView];
}

Does anyone please help guide me where I am missing or doing wrong? That would be greatly appreciated. Thanks in advance!


回答1:


Here I found a solution by changing code for the button in WelcomeVC:

- (IBAction)termsPressed:(id)sender {
    WebViewController *webViewController = [[WebViewController alloc] initWithNibName:nil bundle:nil];
    NSLog(@"Terms of Use");
    webViewController.urlPassed = @"http://.../term.html";

    UINavigationController *webViewNavController =[[UINavigationController alloc] initWithRootViewController:webViewController];

    webViewNavController.navigationBarHidden = NO;

    webViewNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:webViewNavController animated:YES completion:nil];

}



回答2:


Try this:

- (void)viewWillAppear:(BOOL)animated {

    self.navigationController.navigationBarHidden = NO;

}



回答3:


call

[webViewController.navigationController setNavigationBarHidden:NO animated:NO];

In - viewWillAppear method

Also I recommend you update frame with

webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen] bounds]];


来源:https://stackoverflow.com/questions/29899071/navigation-bar-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!