代码改变世界

新的ViewController半透明显示下面的ViewController

2021-03-01 18:14  法子  阅读(207)  评论(0编辑  收藏  举报

参考:https://www.jianshu.com/p/a8b53a126014

关键设置:self.modalPresentationStyle = UIModalPresentationOverCurrentContext;然后把新ViewController的背景色设为半透明:self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];

//storyboard
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    return self;
}
//非storyboard
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self  = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _contentView.transform = CGAffineTransformMakeScale(2, 2);
    [UIView animateWithDuration:0.2 animations:^{
        self.contentView.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

如果是Storyboard跳转,注意