I am trying to create a viewcontroller on the fly and set its view and title bar. Things seems to work just fine. But when I try to set the left button or right button of the navigation bar, they just do not show up. what am I doing wrong?
UIWebView *tempWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[tempWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"someurl"]]];
UIViewController* tempViewController = [[UIViewController alloc]init];
tempViewController.view = tempWebView;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:tempViewController];
UIBarButtonItem* closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:nil];
navigationController.navigationItem.leftBarButtonItem = closeButton;
[self presentViewController:navigationController animated:YES completion:nil];
Change
navigationController.navigationItem.leftBarButtonItem = closeButton;
to
tempViewController.navigationItem.leftBarButtonItem = closeButton;
The navigationItem appearance is handled by the view which is being presented by the nav controller, so if you set it on the UINavigationController but not on the rootViewController, it gets overridden when you push it the first time.
You need to access the navigationItem of tempViewController, not the navigationController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With