文章目录

最近项目中有个需求:要画一个页面,页面的顶部是半透明显示上一个控制器的页面。类似下面这张图:

之前在寻找泡泡效果的弹框时,遇见过,所以知道这个是可以实现的。于是百度了一番,写出来如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//--------------------------------------------
//SGH0524PresentAViewController.m
#import "SGH0524PresentAViewController.h"
#import <Masonry.h>
#import "SGH0524PresentBViewController.h"
@interface SGH0524PresentAViewController ()
@end
@implementation SGH0524PresentAViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = ({
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = [UIFont systemFontOfSize:16];
button.layer.cornerRadius = 3;
button.clipsToBounds = YES;
button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
button.tag = 104;
[button setTitle:@"A Controller Button" forState:UIControlStateNormal];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button addTarget:self action:@selector(p_presentClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button;
});
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(self.view).offset( 100 );
}];
}
-(void)p_presentClick {
SGH0524PresentBViewController *vc = [[SGH0524PresentBViewController alloc]init];
//self.definesPresentationContext = YES;
//大于等于 iOS8.0
if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) {
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else {
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
vc.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
[self presentViewController:vc animated:YES completion:^{ }];
}
@end
//--------------------------------------------
//SGH0524PresentBViewController.m
#import "SGH0524PresentBViewController.h"
#import <Masonry.h>
@interface SGH0524PresentBViewController ()
@end
@implementation SGH0524PresentBViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
UIButton *button = ({
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = [UIFont systemFontOfSize:16];
button.layer.cornerRadius = 3;
button.clipsToBounds = YES;
button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
button.tag = 104;
[button setTitle:@"B Controller Button" forState:UIControlStateNormal];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:button];
button;
});
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(self.view).offset( 150 );
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

效果如下:

来自:iOS学习笔记05 可以看到上一层内容的半透明的界面

文章目录