Popup custom view on current view

很多时候需要在原视图上弹出一个自定义视图让用户确认一些东西. 通常的做法一般使用UIAlertView/UIAlertController. 但是做为自定义它们的风格可能与app不符.


思路如下:

1) 创建一个UIViewController,背景是一个UIImageView,Autolayout至全屏,图片选用半透明的,目标是为了显示前一页的部分内容

2) 通过presentViewController:animated:completion:来弹出视图

3) 注意弹出的视图的背景颜色要改成clearColor,否则无法透明


    viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]){

        viewController.providesPresentationContextTransitionStyle = YES;

        viewController.definesPresentationContext = YES;

        [viewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];

    }

    else{

        [self setModalPresentationStyle:UIModalPresentationCurrentContext];

        [self.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];

    }

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


参考图:

Popup custom view on current view_第1张图片

你可能感兴趣的:(&,Swift)