我正在尝试在iPad的导航栏中为模式添加取消按钮。
我正在使用以下代码:
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];
出于某种原因,取消按钮不会显示。
使用火花检查器,我看到一个后退指示器,其中取消按钮应该是隐藏的,当你在iPad模拟器上运行它时,你看不到取消按钮或后退指示器。
我尝试将后退指示器的隐藏属性设置为NO,但没有显示任何内容。
编辑:我能让它工作的唯一方法是在完成块中添加按钮:
[self presentViewController:navController animated:YES completion:^{abPersonController.navigationItem.leftBarButtonItem = doneButton;}];
但是这个解决方案看起来很糟糕,因为在模态已经在屏幕上之后,完成按钮会弹出。有没有其他方法可以获得它,所以它不必在完成块中?
编辑:
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);
答案 0 :(得分:3)
应该是backBarButtomItem而不是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];
答案 1 :(得分:0)
怎么样:
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)];
在abPersonController
对象的课程-viewDidLoad
(或 init
)中,您可以这样做:
[self.navigationItem setLeftBarButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)]];