导航控制器上的透明模态视图 [英] Transparent Modal View on Navigation Controller

查看:92
本文介绍了导航控制器上的透明模态视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在导航控制器顶部创建透明模式视图。有谁知道这是否可行?

I'm trying to create a transparent modal View on top of my navigation controller. Does anyone know if this is possible?

推荐答案

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

A modal view will cover the view it is pushed on top of as well as the navigation bar for your navigation controller. However, if you use the -presentModalViewController:animated: approach, then once the animation finishes the view just covered will actually disappear, which makes any transparency of your modal view pointless. (You can verify this by implementing the -viewWillDisappear: and -viewDidDisappear: methods in your root view controller).

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

You can add the modal view directly to the view hierarchy like so:

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

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

Adding the modalView as a subview to the root view like this will not actually cover the navigation bar, but it will cover the entire view below it. I tried playing around with the origin of the frame used to init the modalView, but negative values cause it to not display. The best method that I found to cover the entire screen besides the status bar is to add the modalView as a subview of the window itself:

TransparentModalViewAppDelegate *delegate = (TransparentModalViewAppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window addSubview:modalView];

这篇关于导航控制器上的透明模态视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆