Reputation: 461
I'm trying to add a cancel button for the modal in the navigation bar for iPad.
I'm using the following code:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:abPersonController];
navController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)];
[self presentViewController:navController animated:YES completion:nil];
For some reason though, the cancel button does not show up.
Using spark inspector, I'm seeing a back indicator where the cancel button is supposed to be but it is hidden, and when you run it in on iPad Simulator, you cannot see the cancel button or the back indicator.
I tried setting the back indicator's hidden property to NO but nothing shows up.
Edit: The only way I can get it to work is if I add the button in the completion block:
[self presentViewController:navController animated:YES completion:^{abPersonController.navigationItem.leftBarButtonItem = doneButton;}];
But this solution looks bad because the done button pops in after the modal has already been on screen. Are there any other ways to get it so it doesn't have to be in the completion block?
Edit:
UIViewController *dummyView = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:dummyView];
[navController pushViewController:abPersonController animated:NO];
abPersonController.navigationItem.hidesBackButton = YES;
abPersonController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:navController animated:YES completion:nil];
navController.view.superview.bounds = CGRectMake(0, 0, 320, 480);
Upvotes: 0
Views: 3475
Reputation: 461
Should be backBarButtomItem instead of leftBarButtomItem.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:abPersonController];
navController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)];
[self presentViewController:navController animated:YES completion:nil];
Upvotes: 3
Reputation: 20274
How about:
navController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)];
abPersonController.navigationItem.lefBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)];
In abPersonController
object's class -viewDidLoad
(or init
) you can do:
[self.navigationItem setLeftBarButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)]];
Upvotes: 0