Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Added Button does not show on UINavigationController

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];
like image 625
Eyad Fallatah Avatar asked Jun 11 '26 10:06

Eyad Fallatah


2 Answers

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.

like image 59
Gabriele Petronella Avatar answered Jun 13 '26 05:06

Gabriele Petronella


You need to access the navigationItem of tempViewController, not the navigationController.

like image 28
Felix Avatar answered Jun 13 '26 04:06

Felix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!