在模式页面工作表上显示模式表单表



在iPad上,我显示一个带有modalPresentationStyle UIModalPresentationPageSheet的模态视图控制器。此视图控制器使用 modalPresentationStyle UIModalPresentationFormSheet 呈现另一个模态视图控制器。

因此,用户会看到背景视图控制器、页面工作表和表单表都彼此重叠,因为表单页比页页小。页面工作表的演示文稿使背景变暗,因此无法与之交互。但是,表单表不会使 iOS 5 上的页面工作表变暗,因此用户仍然可以与下面的页面工作表进行交互。但是我也希望页面工作表变暗,以便用户必须先关闭模式表单表,然后才能再次与页面工作表交互。

在iOS 4上,

这是默认行为,但在iOS 5上,我找不到实现这一目标的方法。你有什么建议吗?

我相信这是iOS5中的一个错误。我已经做了一些实验来呈现来自其他模态的模态视图控制器,似乎第二个模态永远不会使下面的屏幕变暗。我什至设置了一个测试项目,允许您相互启动无穷无尽的模态,并且似乎其他模态都不会像预期的那样变暗或阻止触摸。

UIWindow子视图上的快速NSLog向我们显示,虽然适当地添加了阴影,但调光视图却没有。我正在研究一种方法来显示我自己的调光视图。当我找到方法时会更新这个答案。

Window Subviews: (
    "<UILayoutContainerView: 0xf63e3c0; frame = (0 0; 768 1024); transform = [0, 1, -1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xf645640>>",
    "<UIDimmingView: 0xf683990; frame = (0 0; 768 1024); opaque = NO; layer = <CALayer: 0xf6836d0>>",
    "<UIDropShadowView: 0xf683130; frame = (64 64; 640 896); transform = [0, 1, -1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xf6831c0>>",
    "<UIDropShadowView: 0x292110; frame = (74 242; 620 540); transform = [0, 1, -1, 0, 0, 0]; autoresize = LM+RM+TM+BM; layer = <CALayer: 0x292150>>"
)

解决方案二:所以在我的动画外观的最终解决方案中。此外,我开始考虑我的第一个解决方案,这在技术上是可行的,这会激怒苹果并导致拒绝,因为 UIDimmingView 没有记录,我们"触摸它"。我添加了一个UIView,背景颜色是我们想要的alpha到我的视图控制器。然后,当我呈现模态时,我对其进行了动画处理,并在调用第二个模态的委托时反转了动画。对我来说看起来还不错。也许一些时间和 alpha 调整以使其恰到好处,但它的工作并且看起来不错。

- (void)viewDidLoad 
{
    dim = [[UIView alloc] init];
    [dim setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.35]];
    [dim setAlpha:0.0];
    [dim setUserInteractionEnabled:NO];
    [self.view addSubview:dim];
}
- (void)presentModal
{
    [self.view bringSubviewToFront:dim];
    [dim setFrame:self.view.frame];
    [UIView animateWithDuration:0.25 animations:^{
        [dim setAlpha:1.0];
    }];
}
- (void)modalDelegateFinished
{
    [UIView animateWithDuration:0.25 animations:^{
        [dim setAlpha:0.0];
    }];
}

解决方案一:

好吧,这有效,但它并不像我想要的那么生动。但是,它确实重用了已经存在的内容,因此可能有一个加分项。

- (void)viewDidAppear:(BOOL)animated
{
    // Add a gesture to dismiss the view if tapped outside.
    UIGesture *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOutsideView:)];
    [gesture setNumberOfTapsRequired:1];
    [gesture setCancelsTouchesInView:NO];
    [self.view.window addGestureRecognizer:gesture];
    // Move the dimmingview to just below the dropshadow.
    UIView *dim = [self.view.window.subviews objectAtIndex:1];
    [self.view.window insertSubview:dim atIndex:2];
}
- (void)tappedOutsideView:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded) {
        CGPoint location = [sender locationInView:nil];
        if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) {
            // remove the gesture on the window
            [self.view.window removeGestureRecognizer:sender];
            // Move the dimmingview back where it belongs
            UIView *dim = [self.view.window.subviews objectAtIndex:2];
            [self.view.window insertSubview:dim atIndex:1];
        }
    }
}

同样作为故障保护,它可能是一个好主意,在视图中的相同内容确实消失了。我的完成按钮调用点击外部视图,因此我知道手势和调光视图始终正确。但是如果你不这样做,你可以把它放在视图中DidDisappear。

- (void)viewDidDisappear:(BOOL)animated
{
    // remove the gesture on the window
    for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
        [self.view.window removeGestureRecognizer:gesture];
    }
    // Move the dimmingview back where it belongs
    UIView *dim = [self.view.window.subviews objectAtIndex:2];
    [self.view.window insertSubview:dim atIndex:1];
}

这似乎确实是一个iOS错误。在iOS7中仍然存在。

这是我为解决它所做的工作(iOS7测试)。有趣的是,在模态上呈现模态不会使演示者变暗,但在这些模态之上呈现的第三种模态会变暗。

知道了这一点,我们可以在呈现实际的表单表模式之前呈现一个假人。这涉及最少的代码,并且在技术上符合一般的 iOS 设计模式。因此,未来爆炸的风险应该很小。

首先展示表单表:

- (void)presentFormSheetModalOverThePageSheetModal
{
    //get the controller I'm trying to present and stick it in a nav controller - as usual
    UIViewController *myModalController = [UIViewController new];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myModalController];
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    //create an intermediate controller that will be presented first
    UIViewController *intermediateController = [UIViewController new];
    intermediateController.modalPresentationStyle = UIModalPresentationCurrentContext;
    intermediateController.view.backgroundColor = self.view.backgroundColor;
    //create a snapshot of self (presenting controller)
    UIView *navBar = [self.navigationController.navigationBar snapshotViewAfterScreenUpdates:YES];
    UIView *mainView = [self.view snapshotViewAfterScreenUpdates:YES];
    mainView.center = CGPointMake(mainView.center.x, mainView.center.y + navBar.frame.size.height);
    [intermediateController.view addSubview:navBar];
    [intermediateController.view addSubview:mainView];
    //two step presentation
    [self presentViewController:intermediateController animated:NO completion:^{
        [intermediateController presentViewController:navController animated:YES completion:nil];
    }];
}

然后,当需要关闭(从呈现的模态中)时:

- (void)dismissMySelf
{
    //again - two step process to dismiss the this controller and the intermediate controller
    UIViewController *originalPresenter = self.presentingViewController.presentingViewController;
    [self.presentingViewController dismissViewControllerAnimated:YES completion:^{
        [originalPresenter dismissViewControllerAnimated:NO completion:nil];
    }];
}

我不得不调整它,因为快照是静态图像并且不处理旋转,但总的来说它对我来说效果很好。希望对您有所帮助。

最新更新