uibarbuttonitemuinavigationitemuiscenedelegate

navigationItem.rightBarButtonItem No Longer Present After Converting from AppDelegate to SceneDelegate


I received a rather ominous warning that my app must be upgraded to support Scenes. So I updated it based on the approach specified in Apple documentation (Managing your app’s life cycle and Specifying the scenes your app supports) which includes the insertion of a UIApplicationSceneManifest in the info.plist as well as the addition of a SceneDelegate.

After allowing my app to conform to the SceneDelegate, the UI for my home screen no longer displays the navigationItem.rightBarButtonItem (and leftBarButtonItem). The initial entry point for my home screen is a UIViewController and all of the non-navigationItem views and buttons are specified in my Storyboard.

However, the navigationItem.rightBarButtonItem (and leftBarButtonItem ) is specified programmatically in my viewDidLoad of the viewController. But they are no longer appearing in the view when the app launches.

The code for the left and right buttons is:

self.navigationItem.leftBarButtonItem = nil;   

UIButton *hamburgerBun = [UIButton buttonWithType:UIButtonTypeCustom];
[hamburgerBun setImage:[[self class] defaultImage] forState:UIControlStateNormal];
[hamburgerBun setFrame: CGRectMake (0, 0, 44.0, 44.0)];
[hamburgerBun addTarget:self action:@selector(toggleLeftPanel) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:hamburgerBun];
buttonItem.style = UIBarButtonItemStylePlain;

self.navigationItem.leftBarButtonItem = buttonItem;


self.navigationItem.righttBarButtonItem = nil;  

UIButton *settingsBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"pencilEditButton.png"];
[settingsBarButton setImage:buttonImage forState:UIControlStateNormal];
[settingsBarButton setFrame: CGRectMake (0, 0, 44.0, 44.0)];
[settingsBarButton addTarget:self action:@selector(editProject:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *settingsBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingsBarButton];
settingsBarButtonItem.style = UIBarButtonItemStylePlain; 

self.navigationItem.rightBarButtonItem = settingsBarButtonItem;

Any ideas what would cause the Navigation Items to no longer be rendered?


Solution

  • Ok, now I understand. I mistakenly made my root view controller be the initial controller when I was upgrading to a scene lifecycle. I did have a navigation controller, but when I made my root view controller the initial controller, it removed the initial controller status from the navigation controller. That prevented the navigation bar item from working. Once I made the navigation controller be the initial controller (as it originally was), the navigation items are once again displaying.