导航控制器上的透明模态视图

lea*_*ter 71 iphone

我正在尝试在导航控制器上创建一个透明的模态视图.有谁知道这是否可能?

pha*_*lat 109

模态视图将覆盖它被推到顶部的视图以及导航控制器的导航栏.但是,如果你使用-presentModalViewController:animated:approach,那么一旦动画结束,刚刚覆盖的视图实际上会消失,这会使你的模态视图的任何透明度毫无意义.(您可以通过在根视图控制器中实现-viewWillDisappear:和-viewDidDisappear:方法来验证这一点).

您可以将模态视图直接添加到视图层次结构中,如下所示:

UIView *modalView =
    [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
modalView.opaque = NO;
modalView.backgroundColor =
    [[UIColor blackColor] colorWithAlphaComponent:0.5f];

UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = @"Modal View";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
[label sizeToFit];
[label setCenter:CGPointMake(modalView.frame.size.width / 2,
                                modalView.frame.size.height / 2)];
[modalView addSubview:label];

[self.view addSubview:modalView];
Run Code Online (Sandbox Code Playgroud)

将modalView作为子视图添加到根视图中,这样实际上不会覆盖导航栏,但它将覆盖它下面的整个视图.我尝试使用用于初始化modalView的框架的原点,但负值导致它不显示.除了状态栏之外,我发现覆盖整个屏幕的最佳方法是将modalView添加为窗口本身的子视图:

TransparentModalViewAppDelegate *delegate = (TransparentModalViewAppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window addSubview:modalView];
Run Code Online (Sandbox Code Playgroud)

  • 最简单的方法是将modalView直接添加到navigationController的视图层次结构中:[self.navigationController.view addSubview:modalView] (4认同)

Mur*_*tam 18

最简单的方法是使用modalPresentationStyle财产navigationController(但你必须自己做动画):

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalViewController animated:NO];
modalViewController.view.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
    modalViewController.view.alpha = 1;
}];
Run Code Online (Sandbox Code Playgroud)


pix*_*x0r 12

我通过设置一个位于窗口或根视图的所有其他子视图之上的"OverlayViewController"来最轻松地完成此任务.在您的app委托或根视图控制器中进行设置,并使OverlayViewController成为单例,以便可以从代码中的任何位置或视图控制器层次结构访问它.然后,您可以随时调用方法来显示模态视图,显示活动指示器等,并且它们可以覆盖任何标签栏或导航控制器.

根视图控制器的示例代码:

- (void)viewDidLoad {
  OverlayViewController *o = [OverlayViewController sharedOverlayViewController];
  [self.view addSubview:o.view];
}
Run Code Online (Sandbox Code Playgroud)

您可能用于显示模态视图的示例代码:

[[OverlayViewController sharedOverlayViewController] presentModalViewController:myModalViewController animated:YES];
Run Code Online (Sandbox Code Playgroud)

我实际上并没有使用-presentModalViewController:animated:我的OverlayViewController,但我希望这可以正常工作.

另请参阅:Objective-C单身人士的样子是什么?


cfi*_*her 8

我有同样的问题,为了解决方案是使用addSubview添加模态视图:并使用UIView的animateWithDuration为视图层次结构中的更改设置动画:delay:options:animations:completion:

我向UIViewController(FRRViewController)的子类添加了一个属性和2个方法,其中包含其他功能.我将很快在gitHub上发布所有内容,但在此之前你可以看到下面的相关代码.有关详细信息,您可以查看我的博客:如何显示透明模态视图控制器.

#pragma mark - Transparent Modal View
-(void) presentTransparentModalViewController: (UIViewController *) aViewController 
                                     animated: (BOOL) isAnimated 
                                    withAlpha: (CGFloat) anAlpha{

    self.transparentModalViewController = aViewController;
    UIView *view = aViewController.view;

    view.opaque = NO;
    view.alpha = anAlpha;
    [view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView *each = obj;
        each.opaque = NO;
        each.alpha = anAlpha;
    }];

    if (isAnimated) {
        //Animated
        CGRect mainrect = [[UIScreen mainScreen] bounds];
        CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);


        [self.view addSubview:view];
        view.frame = newRect;

        [UIView animateWithDuration:0.8
                         animations:^{
                             view.frame = mainrect;
                         } completion:^(BOOL finished) {
                             //nop
                         }];

    }else{
        view.frame = [[UIScreen mainScreen] bounds];
        [self.view addSubview:view];
    }






}

-(void) dismissTransparentModalViewControllerAnimated:(BOOL) animated{

    if (animated) {
        CGRect mainrect = [[UIScreen mainScreen] bounds];
        CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
        [UIView animateWithDuration:0.8
                         animations:^{
                             self.transparentModalViewController.view.frame = newRect;
                         } completion:^(BOOL finished) {
                             [self.transparentModalViewController.view removeFromSuperview];
                             self.transparentModalViewController = nil;
                         }];
    }


}
Run Code Online (Sandbox Code Playgroud)


n13*_*n13 7

以下是我为解决问题所做的工作 - 谷歌细节,但这种方法对我来说非常有用:

  • 截取基础视图的屏幕截图.https://devforums.apple.com/message/266836 - 这导致了一个现成的方法,它返回当前屏幕的UIView.
  • 将屏幕截图移到模态视图(我使用了属性)
  • 提出模态视图
  • 在模态视图控制器的viewDidAppear中,将图像设置为索引0处的UIImageView.按状态栏的高度调整图像的垂直位置.
  • 在模态视图控制器的viewWillDisappear中,再次删除图像

效果是:

  • 视图以任何模态视图进行动画处理 - 模态视图的半透明部分在现有视图上滑动
  • 一旦动画停止,背景就会被设置为屏幕截图 - 这使得它看起来好像旧视图仍在下面,即使它不是.
  • 一旦模态视图的消失动画开始,图像就会被删除.操作系统同时显示旧的导航视图,因此模态视图可以透明地滑动并且如您所期望的那样远离视线.

我尝试在我自己的叠加视图中制作动画,但效果不佳.我得到了一次撞车,但没有迹象表明发生了什么事故.而不是追逐这个,我做了bg视图和工作真的很好.

模态视图中的代码 - 我想你可以弄清楚其余部分,即设置属性modalView.bgImage ...

- (void)viewDidAppear:(BOOL)animated {
    // background
    // Get status bar frame dimensions
    CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:self.bgImage];
    imageView.tag = 5;
    imageView.center = CGPointMake(imageView.center.x, imageView.center.y - statusBarRect.size.height);
    [self.view insertSubview:imageView atIndex:0];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self.view viewWithTag:5] removeFromSuperview];
}
Run Code Online (Sandbox Code Playgroud)