iOS开发17-iOS 模态视图的使用
模态视图控制器并不是一个类,只是一种方式显示的controller。
在程序中切换页面可以使用UINavigationController,也可以使用模态视图。
IOS下的视图控制器都会有一个presentViewController方法,用来显示模态窗口,在一些特别的环境下我们尤其愿意使用这种窗口,例如临时呈现一些内容时(登录视图、分享列表视图等),所以今天在这里做一下整理。
代码:
-(void)buttonAction:(UIButton*)sender{
NextViewController *nextVC = [[NextViewController alloc]init];
nextVC.modalPresentationStyle=UIModalPresentationFormSheet;
nextVC.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
/* 弹出View Controller时的风格
UIModalPresentationFullScreen
UIModalPresentationPageSheet
UIModalPresentationFormSheet
UIModalPresentationCurrentContext
UIModalPresentationCustom
UIModalPresentationOverFullScreen
UIModalPresentationOverCurrentContext
UIModalPresentationPopover
UIModalPresentationNone
*/
/*modalTransitionStyle场景切换动画的风格
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
*/
[self presentModalViewController:nextVC animated:YES];
[self presentViewController:nextVC animated:YES completion:nil];
}
-(void)p_setupViews{
self.view.backgroundColor = [UIColor grayColor];
UIButton *button1 =[UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 100, 150, 30);
[button1 setTitle:@"显示模态视图" forState:UIControlStateNormal];
button1.backgroundColor=[UIColor orangeColor];
[self.view addSubview:button1];
[button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (instancetype)init
{
self = [super init];
if (self) {
[self p_setupViews];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
返回代码:
-(void)buttonAction:(UIButton*)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)p_setupViews{
self.view.backgroundColor = [UIColor redColor];
UIButton *button1 =[UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 100, 80, 30);
[button1 setTitle:@"返回" forState:UIControlStateNormal];
button1.backgroundColor=[UIColor blueColor];
[self.view addSubview:button1];
[button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (instancetype)init
{
self = [super init];
if (self) {
[self p_setupViews];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
效果: