Skip to content

Commit e7cb326

Browse files
committed
optimized alert view edging
1 parent 1f2c2a1 commit e7cb326

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

TYAlertControllerDemo/TYAlertController/TYAlertController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ typedef NS_ENUM(NSInteger, TYAlertTransitionAnimation) {
3737

3838
@property (nonatomic, assign) CGFloat alertViewOriginY; // default center Y
3939

40-
@property (nonatomic, assign) CGFloat alertViewEdging; // only alertStyle, when width frame equal to 0,or no width constraint ,this proprty will use, default to 15
40+
@property (nonatomic, assign) CGFloat alertStyleEdging; // when width frame equal to 0,or no width constraint ,this proprty will use, default to 15 edge
41+
@property (nonatomic, assign) CGFloat actionSheetStyleEdging; // default 0
4142

4243
@property (nonatomic, copy) void (^dismissComplete)(void); // dismiss controller completed block
4344

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ - (void)configureController
176176
self.modalPresentationStyle = UIModalPresentationCustom;
177177
self.transitioningDelegate = self;
178178
_backgoundTapDismissEnable = NO;
179-
_alertViewEdging = 15;
179+
_alertStyleEdging = 15;
180+
_actionSheetStyleEdging = 0;
180181
}
181182

182183
- (void)configureAlertView
@@ -200,17 +201,12 @@ - (void)configureAlertView
200201
}
201202
}
202203

203-
#pragma mark - layout
204-
205-
- (void)layoutAlertStyleView
204+
- (void)configureAlertViewWidth
206205
{
207-
// center X
208-
[self.view addConstraintCenterXToView:_alertView CenterYToView:nil];
209-
210206
// width, height
211207
if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
212208
[_alertView addConstarintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];
213-
209+
214210
}else {
215211
BOOL findAlertViewWidthConstraint = NO;
216212
for (NSLayoutConstraint *constraint in _alertView.constraints) {
@@ -221,9 +217,19 @@ - (void)layoutAlertStyleView
221217
}
222218

223219
if (!findAlertViewWidthConstraint) {
224-
[_alertView addConstarintWidth:CGRectGetWidth(self.view.frame)-2*_alertViewEdging height:0];
220+
[_alertView addConstarintWidth:CGRectGetWidth(self.view.frame)-2*_alertStyleEdging height:0];
225221
}
226222
}
223+
}
224+
225+
#pragma mark - layout
226+
227+
- (void)layoutAlertStyleView
228+
{
229+
// center X
230+
[self.view addConstraintCenterXToView:_alertView CenterYToView:nil];
231+
232+
[self configureAlertViewWidth];
227233

228234
// top Y
229235
_alertViewCenterYConstraint = [self.view addConstraintCenterYToView:_alertView constant:0];
@@ -242,7 +248,7 @@ - (void)layoutActionSheetStyleView
242248
// center X
243249
[self.view addConstraintCenterXToView:_alertView CenterYToView:nil];
244250

245-
[self.view addConstarintWithView:_alertView topView:nil leftView:self.view bottomView:self.view rightView:self.view edageInset:UIEdgeInsetsZero];
251+
[self.view addConstarintWithView:_alertView topView:nil leftView:self.view bottomView:self.view rightView:self.view edageInset:UIEdgeInsetsMake(0, _actionSheetStyleEdging, 0, -_actionSheetStyleEdging)];
246252

247253
if (CGRectGetHeight(_alertView.frame) > 0) {
248254
// height

TYAlertControllerDemo/TYAlertController/TYAlertView.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ typedef NS_ENUM(NSUInteger, TYAlertActionStyle) {
3030
@property (nonatomic, weak, readonly) UILabel *titleLable;
3131
@property (nonatomic, weak, readonly) UILabel *messageLabel;
3232

33+
// default 280, if 0 don't add width constraint,
34+
@property (nonatomic, assign) CGFloat alertViewWidth;
35+
3336
// contentView space custom
3437
@property (nonatomic, assign) CGFloat contentViewSpace;
3538

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ @interface TYAlertView ()
6666

6767
@end
6868

69+
#define kAlertViewWidth 280
6970
#define kContentViewEdge 15
7071
#define kContentViewSpace 15
7172

@@ -120,6 +121,7 @@ + (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message
120121
- (void)configureProperty
121122
{
122123
self.backgroundColor = [UIColor whiteColor];
124+
_alertViewWidth = kAlertViewWidth;
123125
_contentViewSpace = kContentViewSpace;
124126

125127
_textLabelSpace = kTextLabelSpace;
@@ -271,6 +273,10 @@ - (void)layoutContentViews
271273
// layout done
272274
return;
273275
}
276+
if (_alertViewWidth) {
277+
[self addConstarintWidth:_alertViewWidth height:0];
278+
}
279+
274280
// textContentView
275281
_textContentView.translatesAutoresizingMaskIntoConstraints = NO;
276282

TYAlertControllerDemo/ViewController.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ - (IBAction)blurEffectAlertViewAction:(id)sender {
104104

105105

106106
- (IBAction)costomAlertViewAction:(id)sender {
107-
107+
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"message" message:@"this is system alertController" preferredStyle:UIAlertControllerStyleActionSheet];
108+
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
109+
110+
}]];
111+
[self presentViewController:alertController animated:YES completion:nil];
108112
}
109113

110114

0 commit comments

Comments
 (0)