PresentModalViewController显示半透明UIView

很多时候我们需要使用presentModalViewController来显示Modal View。如果需要显示半透明的Modal View应该怎么办呢?当然可以自己创建一个半透明的UIView,然后模拟presentModalViewController的动画效果。

不过iOS 4以后的版本再也不需要怎么麻烦了,有一个非常简单的方法,示例如下(这段代码运行于一个View Controller中):

 UIViewController* transparentView = [[UIViewController alloc] init];            

       UIViewController* controller = self.view.window.rootViewController;
       transparentView.view.backgroundColor = [UIColor clearColor];
       controller.modalPresentationStyle = UIModalPresentationCurrentContext;        
       [controller presentModalViewController:transparentView animated:YES];

其要点就是使用iOS特有的rootViewController来显示Modal View。

你可能感兴趣的:(ios,UIView)