我正试图在我的导航控制器上创建一个透明的模式视图。有没有人知道这是否可能?
发布于 2009-05-13 17:10:02
模式视图将覆盖它被推送到其顶部的视图以及导航控制器的导航栏。但是,如果您使用-presentModalViewController:animated:方法,那么一旦动画完成,刚刚覆盖的视图实际上将消失,这使得您的模式视图的任何透明度都没有意义。(您可以通过在根视图控制器中实现-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];像这样将modalView作为子视图添加到根视图中实际上不会覆盖导航栏,但会覆盖导航栏下面的整个视图。我尝试使用用于初始化modalView的帧的原点,但负值导致它不显示。除了状态栏之外,我发现覆盖整个屏幕的最好方法是将modalView添加为窗口本身的子视图:
TransparentModalViewAppDelegate *delegate = (TransparentModalViewAppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window addSubview:modalView];发布于 2012-09-10 05:07:13
最简单的方法是使用navigationController的modalPresentationStyle属性(但您必须自己制作动画):
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalViewController animated:NO];
modalViewController.view.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
modalViewController.view.alpha = 1;
}];发布于 2009-05-11 18:45:58
我最容易做到这一点的方法是设置一个"OverlayViewController“,它位于我的窗口或根视图的所有其他子视图之上。在您的应用程序委托或根视图控制器中设置它,并将OverlayViewController设置为单例,以便可以从代码或视图控制器层次结构中的任何位置访问它。然后,您可以在需要时调用方法来显示模式视图、显示活动指示器等,并且它们可能会覆盖任何选项卡栏或导航控制器。
根视图控制器的示例代码:
- (void)viewDidLoad {
OverlayViewController *o = [OverlayViewController sharedOverlayViewController];
[self.view addSubview:o.view];
}可以用来显示模式视图的示例代码:
[[OverlayViewController sharedOverlayViewController] presentModalViewController:myModalViewController animated:YES];我还没有在我的OverlayViewController中实际使用过-presentModalViewController:animated:,但我希望这可以很好地工作。
https://stackoverflow.com/questions/849458
复制相似问题