搜索
您的当前位置:首页正文

iOS 模态出一个半透明的controller

来源:哗拓教育

有时候需要一个半透明的视图, 之前就用UIView实现的,后来发现了一个更好的方法,就是模态一个控制器,这样代码可以更好的分离了,效果其实跟系统自带弹窗差不多。

分两种情况:

一、present 一个 UIViewController

self.definesPresentationContext = YES;

LiveInfoSelectViewController * liveInfoSelectVC = [[LiveInfoSelectViewController alloc] init];

liveInfoSelectVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];

liveInfoSelectVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

liveInfoSelectVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[self presentViewController:liveInfoSelectVC animated:YES completion:nil];

二、present 一个 UINavigationController,用于还需要push跳转的情况

self.definesPresentationContext = YES;

SignInViewController *signInVC = [[SignInViewController alloc] init];

signInVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:signInVC];

nav.modalPresentationStyle = UIModalPresentationOverFullScreen;

nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

nav.view.backgroundColor = [UIColor clearColor];

[self presentViewController:nav animated:YES completion:nil];

modalPresentationStyle和modalTransitionStyle这两个属性,自己可以试一下,看看都是什么效果,另外backgroundColor也可以在 viewDidLoad 中进行设置。最后上个图吧

这个弹窗就是一个背景半透明的控制器实现的
Top