7
7
//
8
8
9
9
#import " TYAlertController.h"
10
+ #import " UIView+TYAutoLayout.h"
10
11
11
12
@interface TYAlertController ()
12
13
@@ -79,16 +80,30 @@ + (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:
79
80
- (void )viewDidLoad {
80
81
[super viewDidLoad ];
81
82
// Do any additional setup after loading the view.
82
- self.view .backgroundColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.4 ];
83
+ self.view .backgroundColor = [UIColor clearColor ];
84
+ [self addBackgroundView ];
83
85
84
86
[self addSingleTapGesture ];
85
87
86
88
[self configureAlertView ];
89
+
90
+ [self .view layoutIfNeeded ];
91
+
92
+ }
93
+
94
+ - (UIView *)backgroundView
95
+ {
96
+ if (_backgroundView == nil ) {
97
+ _backgroundView = [[UIView alloc ]init];
98
+ _backgroundView.backgroundColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.4 ];
99
+ }
100
+ return _backgroundView;
87
101
}
88
102
89
- - (void )viewWillAppear : ( BOOL ) animated
103
+ - (void )addBackgroundView
90
104
{
91
- [super viewWillAppear: animated];
105
+ self.backgroundView .frame = self.view .bounds ;
106
+ [self .view addSubview: self .backgroundView];
92
107
}
93
108
94
109
- (void )addSingleTapGesture
@@ -105,7 +120,7 @@ - (void)addSingleTapGesture
105
120
// 点击次数
106
121
singleTap.numberOfTapsRequired = 1 ;
107
122
// 增加事件者响应者,
108
- [self .view addGestureRecognizer: singleTap];
123
+ [self .backgroundView addGestureRecognizer: singleTap];
109
124
}
110
125
111
126
- (void )configureController
@@ -144,21 +159,20 @@ - (void)configureAlertView
144
159
- (void )layoutAlertStyleView
145
160
{
146
161
// center X
147
- NSLayoutConstraint *alertViewCenterXConstraint = [ NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: self .view attribute: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0 ];
162
+ [ self .view addConstraintCenterXToView: _alertView CenterYToView: nil ];
148
163
149
164
// top Y
150
- NSLayoutConstraint *alertViewTopYConstraint = nil ;
151
165
if (_alertViewOriginY > 0 ) {
152
- alertViewTopYConstraint = [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem: self .view attribute: NSLayoutAttributeTop multiplier: 1.0 constant: _alertViewOriginY];
166
+
167
+ [self .view addConstarintWithView: _alertView topView: self .view leftView: nil bottomView: nil rightView: nil edageInset: UIEdgeInsetsMake (_alertViewOriginY, 0 , 0 , 0 )];
153
168
}else {
154
- alertViewTopYConstraint = [ NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem: self .view attribute: NSLayoutAttributeCenterY multiplier: 1.0 constant: 0 ];
169
+ [ self .view addConstraintCenterXToView: nil CenterYToView: _alertView ];
155
170
}
156
171
157
172
if (!CGSizeEqualToSize (_alertView.frame .size ,CGSizeZero)) {
158
- // height
159
- [_alertView addConstraint: [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute: 0 multiplier: 1 constant: CGRectGetHeight (_alertView.frame)]];
160
173
// width
161
- [_alertView addConstraint: [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: 0 multiplier: 1 constant: CGRectGetWidth (_alertView.frame)]];
174
+ [_alertView addConstarintWidth: CGRectGetWidth (_alertView.frame) height: CGRectGetHeight (_alertView.frame)];
175
+
162
176
}else {
163
177
BOOL findAlertViewWidthConstraint = NO ;
164
178
for (NSLayoutConstraint *constraint in _alertView.constraints ) {
@@ -169,31 +183,23 @@ - (void)layoutAlertStyleView
169
183
}
170
184
171
185
if (!findAlertViewWidthConstraint) {
172
- // width
173
- [_alertView addConstraint: [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: 0 multiplier: 1 constant: CGRectGetWidth (self .view.frame)-2 *_alertViewEdging]];
186
+ [_alertView addConstarintWidth: CGRectGetWidth (self .view.frame)-2 *_alertViewEdging height: 0 ];
174
187
}
175
188
}
176
-
177
- [self .view addConstraints: @[alertViewCenterXConstraint,alertViewTopYConstraint]];
178
189
}
179
190
180
191
- (void )layoutActionSheetStyleView
181
192
{
182
193
// center X
183
- NSLayoutConstraint *alertViewCenterXConstraint = [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: self .view attribute: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0 ];
184
- // Bottom
185
- NSLayoutConstraint *alertViewButtomYConstraint = [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeBottom relatedBy: NSLayoutRelationEqual toItem: self .view attribute: NSLayoutAttributeBottom multiplier: 1.0 constant: 0 ];
186
- // left
187
- NSLayoutConstraint *alertViewLeftConstraint = [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeLeft relatedBy: NSLayoutRelationEqual toItem: self .view attribute: NSLayoutAttributeLeft multiplier: 1.0 constant: 0 ];
188
- // right
189
- NSLayoutConstraint *alertViewRightConstraint = [NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeRight relatedBy: NSLayoutRelationEqual toItem: self .view attribute: NSLayoutAttributeRight multiplier: 1.0 constant: 0 ];
194
+ [self .view addConstraintCenterXToView: _alertView CenterYToView: nil ];
195
+
196
+ [self .view addConstarintWithView: _alertView topView: nil leftView: self .view bottomView: self .view rightView: self .view edageInset: UIEdgeInsetsZero];
190
197
191
198
if (CGRectGetHeight (_alertView.frame ) > 0 ) {
192
199
// height
193
- [_alertView addConstraint: [ NSLayoutConstraint constraintWithItem: _alertView attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute: 0 multiplier: 1 constant: CGRectGetHeight (_alertView.frame)] ];
200
+ [_alertView addConstarintWidth: 0 height: CGRectGetHeight (_alertView.frame)];
194
201
}
195
-
196
- [self .view addConstraints: @[alertViewCenterXConstraint,alertViewButtomYConstraint,alertViewLeftConstraint,alertViewRightConstraint]];
202
+
197
203
}
198
204
199
205
#pragma mark - action
@@ -208,4 +214,9 @@ - (void)didReceiveMemoryWarning {
208
214
// Dispose of any resources that can be recreated.
209
215
}
210
216
217
+ - (void )dealloc
218
+ {
219
+ NSLog (@" TYAlertController dealloc" );
220
+ }
221
+
211
222
@end
0 commit comments