简体   繁体   中英

Cancel button not showing up in UINavigationController in modal for iPad

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);

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];

How about:

1.

navController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                                      target:self
                                                                                                      action:@selector(dismissModal)];

2.

abPersonController.navigationItem.lefBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                                   target:self
                                                                                                   action:@selector(dismissModal)];

Or...

In abPersonController object's class -viewDidLoad (or init ) you can do:

[self.navigationItem setLeftBarButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                    target:self
                                                                                    action:@selector(dismissModal)]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM