默认情况下,UISplitViewController提供滑动手势以纵向模式打开主视图.
不幸的是,即使操作详细页面上的滑块,此手势也会打开主控制器!我在这里发现了另一个问题,这个问题被标记为iOS 5.1报告的错误,但问题仍然存在于iOS7上……
现在有办法在这种情况下抑制这种姿势吗?我意识到我可以通过这样做完全删除它:
splitViewController.presentsWithGesture = NO;
然而,允许手势并仍然有可用的滑块将是很好的!
解决方法
在试验之后,我想知道如果我尝试创建自己的滑动手势来打开主视图会发生什么.这非常有效,使用滑块时不会失败!
所以,在app委托中我禁止默认滑动手势:
splitViewController.presentsWithGesture = NO;
加载详细信息视图后,我创建了一个滑动手势:
- (void)viewDidLoad { [super viewDidLoad]; UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)]; swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeGestureRecognizer]; [self configureView]; }
在splitViewController中:willHideViewController:withBarButtonItem:forPopoverController:我存储了自己打开主视图所需的引用:
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController { // Add the bar item to the navigation bar barButtonItem.title = NSLocalizedString(@"Master",@"Master"); [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES]; // Store references to the button and popover controller so that we can manually open the view using a custome swipe gesture self.masterPopoverButton = barButtonItem; self.masterPopoverController = popoverController; }
最后,我处理滑动:
- (void)handleRightSwipe:(UISwipeGestureRecognizer *)recognizer { // Find the root controller in the stack (this is the one that's also the split view's delegate,and thus has access // to the pop over controller. MyDetailViewController *rootController = (MyDetailViewController *)[self.navigationController.viewControllers objectAtIndex:0]; if (!rootController.masterPopoverController.popoverVisible) { [rootController.masterPopoverController presentPopoverFromBarButtonItem:rootController.masterPopoverButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } }
这将在主视图控制器中滑动.我担心这会将控制器显示为传统的弹出窗口(带箭头等),但它实际上是正确的(至少它在iOS 7下 – 我没有测试过早期版本).
请注意,您需要为推送到导航控制器堆栈的每个视图创建此手势.在我的情况下,它始终是相同的视图,因此简化了事情.在其他情况下,最好创建一个UIViewController的子类来创建这个手势并处理它,然后将其用作任何被推送的控制器的超类……