-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathQMUIAlertController.m
More file actions
1301 lines (1114 loc) · 62.1 KB
/
QMUIAlertController.m
File metadata and controls
1301 lines (1114 loc) · 62.1 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// QMUIAlertController.m
// qmui
//
// Created by QMUI Team on 15/7/20.
//
#import "QMUIAlertController.h"
#import "QMUICore.h"
#import "QMUIButton.h"
#import "QMUITextField.h"
#import "UIView+QMUI.h"
#import "UIControl+QMUI.h"
#import "NSParagraphStyle+QMUI.h"
#import "UIImage+QMUI.h"
#import "CALayer+QMUI.h"
#import "QMUIKeyboardManager.h"
#import "QMUIAppearance.h"
#import "QMUILabel.h"
static NSUInteger alertControllerCount = 0;
#pragma mark - QMUIBUttonWrapView
@interface QMUIAlertButtonWrapView : UIView
@property(nonatomic, strong) QMUIButton *button;
@end
@implementation QMUIAlertButtonWrapView
- (instancetype)init {
self = [super init];
if (self) {
self.button = [[QMUIButton alloc] init];
self.button.adjustsButtonWhenDisabled = NO;
self.button.adjustsButtonWhenHighlighted = NO;
[self addSubview:self.button];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.button.frame = self.bounds;
}
@end
#pragma mark - QMUIAlertAction
@protocol QMUIAlertActionDelegate <NSObject>
- (void)didClickAlertAction:(QMUIAlertAction *)alertAction;
@end
@interface QMUIAlertAction ()
@property(nonatomic, copy, readwrite) NSString *title;
@property(nonatomic, assign, readwrite) QMUIAlertActionStyle style;
@property(nonatomic, copy) void (^handler)(QMUIAlertController *aAlertController, QMUIAlertAction *action);
@property(nonatomic, weak) id<QMUIAlertActionDelegate> delegate;
@end
@implementation QMUIAlertAction
+ (nonnull instancetype)actionWithTitle:(nullable NSString *)title style:(QMUIAlertActionStyle)style handler:(void (^)(__kindof QMUIAlertController *, QMUIAlertAction *))handler {
QMUIAlertAction *alertAction = [[self alloc] init];
alertAction.title = title;
alertAction.style = style;
alertAction.handler = handler;
return alertAction;
}
- (nonnull instancetype)init {
self = [super init];
if (self) {
_button = [[QMUIButton alloc] init];
self.button.adjustsButtonWhenDisabled = NO;
self.button.adjustsButtonWhenHighlighted = NO;
self.button.qmui_automaticallyAdjustTouchHighlightedInScrollView = YES;
[self.button addTarget:self action:@selector(handleAlertActionEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)setEnabled:(BOOL)enabled {
_enabled = enabled;
self.button.enabled = enabled;
}
- (void)handleAlertActionEvent:(id)sender {
// 需要先调delegate,里面会先恢复keywindow
if (self.delegate && [self.delegate respondsToSelector:@selector(didClickAlertAction:)]) {
[self.delegate didClickAlertAction:self];
}
}
@end
@implementation QMUIAlertController (UIAppearance)
+ (instancetype)appearance {
return [QMUIAppearance appearanceForClass:self];
}
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self initAppearance];
});
}
+ (void)initAppearance {
QMUIAlertController *alertControllerAppearance = QMUIAlertController.appearance;
alertControllerAppearance.alertContentMargin = UIEdgeInsetsMake(0, 0, 0, 0);
alertControllerAppearance.alertContentMaximumWidth = 270;
alertControllerAppearance.alertSeparatorColor = UIColorMake(211, 211, 219);
alertControllerAppearance.alertTitleAttributes = @{NSForegroundColorAttributeName:UIColorBlack,NSFontAttributeName:UIFontBoldMake(17),NSParagraphStyleAttributeName:[NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:0 lineBreakMode:NSLineBreakByTruncatingTail textAlignment:NSTextAlignmentCenter]};
alertControllerAppearance.alertMessageAttributes = @{NSForegroundColorAttributeName:UIColorBlack,NSFontAttributeName:UIFontMake(13),NSParagraphStyleAttributeName:[NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:0 lineBreakMode:NSLineBreakByTruncatingTail textAlignment:NSTextAlignmentCenter]};
alertControllerAppearance.alertButtonAttributes = @{NSForegroundColorAttributeName:UIColorBlue,NSFontAttributeName:UIFontMake(17),NSKernAttributeName:@(0)};
alertControllerAppearance.alertButtonDisabledAttributes = @{NSForegroundColorAttributeName:UIColorMake(129, 129, 129),NSFontAttributeName:UIFontMake(17),NSKernAttributeName:@(0)};
alertControllerAppearance.alertCancelButtonAttributes = @{NSForegroundColorAttributeName:UIColorBlue,NSFontAttributeName:UIFontBoldMake(17),NSKernAttributeName:@(0)};
alertControllerAppearance.alertDestructiveButtonAttributes = @{NSForegroundColorAttributeName:UIColorRed,NSFontAttributeName:UIFontMake(17),NSKernAttributeName:@(0)};
alertControllerAppearance.alertContentCornerRadius = 13;
alertControllerAppearance.alertButtonHeight = 44;
alertControllerAppearance.alertHeaderBackgroundColor = UIColorMakeWithRGBA(247, 247, 247, 1);
alertControllerAppearance.alertButtonBackgroundColor = alertControllerAppearance.alertHeaderBackgroundColor;
alertControllerAppearance.alertButtonHighlightBackgroundColor = UIColorMake(232, 232, 232);
alertControllerAppearance.alertHeaderInsets = UIEdgeInsetsMake(20, 16, 20, 16);
alertControllerAppearance.alertTitleMessageSpacing = 3;
alertControllerAppearance.alertTextFieldFont = UIFontMake(14);
alertControllerAppearance.alertTextFieldTextColor = UIColorBlack;
alertControllerAppearance.alertTextFieldBorderColor = UIColorMake(210, 210, 210);
alertControllerAppearance.alertTextFieldTextInsets = UIEdgeInsetsMake(4, 7, 4, 7);
alertControllerAppearance.sheetContentMargin = UIEdgeInsetsMake(10, 10, 10, 10);
alertControllerAppearance.sheetContentMaximumWidth = [QMUIHelper screenSizeFor55Inch].width - UIEdgeInsetsGetHorizontalValue(alertControllerAppearance.sheetContentMargin);
alertControllerAppearance.sheetSeparatorColor = UIColorMake(211, 211, 219);
alertControllerAppearance.sheetTitleAttributes = @{NSForegroundColorAttributeName:UIColorMake(143, 143, 143),NSFontAttributeName:UIFontBoldMake(13),NSParagraphStyleAttributeName:[NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:0 lineBreakMode:NSLineBreakByTruncatingTail textAlignment:NSTextAlignmentCenter]};
alertControllerAppearance.sheetMessageAttributes = @{NSForegroundColorAttributeName:UIColorMake(143, 143, 143),NSFontAttributeName:UIFontMake(13),NSParagraphStyleAttributeName:[NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:0 lineBreakMode:NSLineBreakByTruncatingTail textAlignment:NSTextAlignmentCenter]};
alertControllerAppearance.sheetButtonAttributes = @{NSForegroundColorAttributeName:UIColorBlue,NSFontAttributeName:UIFontMake(20),NSKernAttributeName:@(0)};
alertControllerAppearance.sheetButtonDisabledAttributes = @{NSForegroundColorAttributeName:UIColorMake(129, 129, 129),NSFontAttributeName:UIFontMake(20),NSKernAttributeName:@(0)};
alertControllerAppearance.sheetCancelButtonAttributes = @{NSForegroundColorAttributeName:UIColorBlue,NSFontAttributeName:UIFontBoldMake(20),NSKernAttributeName:@(0)};
alertControllerAppearance.sheetDestructiveButtonAttributes = @{NSForegroundColorAttributeName:UIColorRed,NSFontAttributeName:UIFontMake(20),NSKernAttributeName:@(0)};
alertControllerAppearance.sheetCancelButtonMarginTop = 8;
alertControllerAppearance.sheetContentCornerRadius = 13;
alertControllerAppearance.sheetButtonHeight = 57;
alertControllerAppearance.sheetHeaderBackgroundColor = UIColorMakeWithRGBA(247, 247, 247, 1);
alertControllerAppearance.sheetButtonBackgroundColor = alertControllerAppearance.sheetHeaderBackgroundColor;
alertControllerAppearance.sheetButtonHighlightBackgroundColor = UIColorMake(232, 232, 232);
alertControllerAppearance.sheetHeaderInsets = UIEdgeInsetsMake(16, 16, 16, 16);
alertControllerAppearance.sheetTitleMessageSpacing = 8;
alertControllerAppearance.sheetButtonColumnCount = 1;
alertControllerAppearance.isExtendBottomLayout = NO;
}
@end
#pragma mark - QMUIAlertController
@interface QMUIAlertController () <QMUIAlertActionDelegate, QMUIModalPresentationContentViewControllerProtocol, QMUIModalPresentationViewControllerDelegate, QMUITextFieldDelegate>
@property(nonatomic, assign, readwrite) QMUIAlertControllerStyle preferredStyle;
@property(nonatomic, strong, readwrite) QMUIModalPresentationViewController *modalPresentationViewController;
@property(nonatomic, strong) UIView *containerView;
@property(nonatomic, strong) UIControl *dimmingView;
@property(nonatomic, strong) UIView *scrollWrapView;
@property(nonatomic, strong) UIScrollView *headerScrollView;
@property(nonatomic, strong) UIScrollView *buttonScrollView;
@property(nonatomic, strong) CALayer *extendLayer;
@property(nonatomic, strong) QMUILabel *titleLabel;
@property(nonatomic, strong) QMUILabel *messageLabel;
@property(nonatomic, strong) QMUIAlertAction *cancelAction;
@property(nonatomic, strong) NSMutableArray<QMUIAlertAction *> *alertActions;
@property(nonatomic, strong) NSMutableArray<QMUIAlertAction *> *destructiveActions;
@property(nonatomic, strong) NSMutableArray<UITextField *> *alertTextFields;
@property(nonatomic, assign) CGFloat keyboardHeight;
/// 调用 showWithAnimated 时置为 YES,在 show 动画结束时置为 NO
@property(nonatomic, assign) BOOL willShow;
/// 在 show 动画结束时置为 YES,在 hide 动画结束时置为 NO
@property(nonatomic, assign) BOOL showing;
// 保护 showing 的过程中调用 hide 无效
@property(nonatomic, assign) BOOL isNeedsHideAfterAlertShowed;
@property(nonatomic, assign) BOOL isAnimatedForHideAfterAlertShowed;
@end
@implementation QMUIAlertController {
NSString *_title;
BOOL _needsUpdateAction;
BOOL _needsUpdateTitle;
BOOL _needsUpdateMessage;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self didInitialize];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self didInitialize];
}
return self;
}
- (void)didInitialize {
[self qmui_applyAppearance];
self.alertTextFieldMarginBlock = ^UIEdgeInsets(__kindof QMUIAlertController *aAlertController, NSInteger aTextFieldIndex) {
if (aTextFieldIndex == aAlertController.textFields.count - 1) {
return UIEdgeInsetsMake(0, 0, 16, 0);
}
return UIEdgeInsetsZero;
};
self.shouldManageTextFieldsReturnEventAutomatically = YES;
}
- (void)setAlertButtonAttributes:(NSDictionary<NSString *,id> *)alertButtonAttributes {
_alertButtonAttributes = alertButtonAttributes;
_needsUpdateAction = YES;
}
- (void)setSheetButtonAttributes:(NSDictionary<NSString *,id> *)sheetButtonAttributes {
_sheetButtonAttributes = sheetButtonAttributes;
_needsUpdateAction = YES;
}
- (void)setAlertButtonDisabledAttributes:(NSDictionary<NSString *,id> *)alertButtonDisabledAttributes {
_alertButtonDisabledAttributes = alertButtonDisabledAttributes;
_needsUpdateAction = YES;
}
- (void)setSheetButtonDisabledAttributes:(NSDictionary<NSString *,id> *)sheetButtonDisabledAttributes {
_sheetButtonDisabledAttributes = sheetButtonDisabledAttributes;
_needsUpdateAction = YES;
}
- (void)setAlertCancelButtonAttributes:(NSDictionary<NSString *,id> *)alertCancelButtonAttributes {
_alertCancelButtonAttributes = alertCancelButtonAttributes;
_needsUpdateAction = YES;
}
- (void)setSheetCancelButtonAttributes:(NSDictionary<NSString *,id> *)sheetCancelButtonAttributes {
_sheetCancelButtonAttributes = sheetCancelButtonAttributes;
_needsUpdateAction = YES;
}
- (void)setAlertDestructiveButtonAttributes:(NSDictionary<NSString *,id> *)alertDestructiveButtonAttributes {
_alertDestructiveButtonAttributes = alertDestructiveButtonAttributes;
_needsUpdateAction = YES;
}
- (void)setSheetDestructiveButtonAttributes:(NSDictionary<NSString *,id> *)sheetDestructiveButtonAttributes {
_sheetDestructiveButtonAttributes = sheetDestructiveButtonAttributes;
_needsUpdateAction = YES;
}
- (void)setAlertButtonBackgroundColor:(UIColor *)alertButtonBackgroundColor {
_alertButtonBackgroundColor = alertButtonBackgroundColor;
_needsUpdateAction = YES;
}
- (void)setSheetButtonBackgroundColor:(UIColor *)sheetButtonBackgroundColor {
_sheetButtonBackgroundColor = sheetButtonBackgroundColor;
[self updateExtendLayerAppearance];
_needsUpdateAction = YES;
}
- (void)setAlertButtonHighlightBackgroundColor:(UIColor *)alertButtonHighlightBackgroundColor {
_alertButtonHighlightBackgroundColor = alertButtonHighlightBackgroundColor;
_needsUpdateAction = YES;
}
- (void)setSheetButtonHighlightBackgroundColor:(UIColor *)sheetButtonHighlightBackgroundColor {
_sheetButtonHighlightBackgroundColor = sheetButtonHighlightBackgroundColor;
_needsUpdateAction = YES;
}
- (void)setAlertTitleAttributes:(NSDictionary<NSString *,id> *)alertTitleAttributes {
_alertTitleAttributes = alertTitleAttributes;
_needsUpdateTitle = YES;
}
- (void)setAlertMessageAttributes:(NSDictionary<NSString *,id> *)alertMessageAttributes {
_alertMessageAttributes = alertMessageAttributes;
_needsUpdateMessage = YES;
}
- (void)setSheetTitleAttributes:(NSDictionary<NSString *,id> *)sheetTitleAttributes {
_sheetTitleAttributes = sheetTitleAttributes;
_needsUpdateTitle = YES;
}
- (void)setSheetMessageAttributes:(NSDictionary<NSString *,id> *)sheetMessageAttributes {
_sheetMessageAttributes = sheetMessageAttributes;
_needsUpdateMessage = YES;
}
- (void)setAlertHeaderBackgroundColor:(UIColor *)alertHeaderBackgroundColor {
_alertHeaderBackgroundColor = alertHeaderBackgroundColor;
[self updateHeaderBackgrondColor];
}
- (void)setSheetHeaderBackgroundColor:(UIColor *)sheetHeaderBackgroundColor {
_sheetHeaderBackgroundColor = sheetHeaderBackgroundColor;
[self updateHeaderBackgrondColor];
}
- (void)updateHeaderBackgrondColor {
if (self.preferredStyle == QMUIAlertControllerStyleActionSheet) {
if (_headerScrollView) { _headerScrollView.backgroundColor = self.sheetHeaderBackgroundColor; }
} else if (self.preferredStyle == QMUIAlertControllerStyleAlert) {
if (_headerScrollView) { _headerScrollView.backgroundColor = self.alertHeaderBackgroundColor; }
}
}
- (void)setAlertSeparatorColor:(UIColor *)alertSeparatorColor {
_alertSeparatorColor = alertSeparatorColor;
[self updateSeparatorColor];
}
- (void)setSheetSeparatorColor:(UIColor *)sheetSeparatorColor {
_sheetSeparatorColor = sheetSeparatorColor;
[self updateSeparatorColor];
}
- (void)updateSeparatorColor {
UIColor *separatorColor = self.preferredStyle == QMUIAlertControllerStyleAlert ? self.alertSeparatorColor : self.sheetSeparatorColor;
[self.alertActions enumerateObjectsUsingBlock:^(QMUIAlertAction * _Nonnull alertAction, NSUInteger idx, BOOL * _Nonnull stop) {
alertAction.button.qmui_borderColor = separatorColor;
}];
}
- (void)setAlertContentCornerRadius:(CGFloat)alertContentCornerRadius {
_alertContentCornerRadius = alertContentCornerRadius;
[self updateCornerRadius];
}
- (void)setSheetContentCornerRadius:(CGFloat)sheetContentCornerRadius {
_sheetContentCornerRadius = sheetContentCornerRadius;
[self updateCornerRadius];
}
- (void)setIsExtendBottomLayout:(BOOL)isExtendBottomLayout {
_isExtendBottomLayout = isExtendBottomLayout;
if (isExtendBottomLayout) {
self.extendLayer.hidden = NO;
[self updateExtendLayerAppearance];
} else {
self.extendLayer.hidden = YES;
}
}
- (void)updateExtendLayerAppearance {
if (_extendLayer) {
_extendLayer.backgroundColor = self.sheetButtonBackgroundColor.CGColor;
}
}
- (void)updateCornerRadius {
if (self.preferredStyle == QMUIAlertControllerStyleAlert) {
if (self.containerView) { self.containerView.layer.cornerRadius = self.alertContentCornerRadius; self.containerView.clipsToBounds = YES; }
if (self.cancelButtonVisualEffectView) { self.cancelButtonVisualEffectView.layer.cornerRadius = self.alertContentCornerRadius; self.cancelButtonVisualEffectView.clipsToBounds = NO;}
if (self.scrollWrapView) { self.scrollWrapView.layer.cornerRadius = 0; self.scrollWrapView.clipsToBounds = NO; }
} else {
if (self.containerView) { self.containerView.layer.cornerRadius = 0; self.containerView.clipsToBounds = NO; }
if (self.cancelButtonVisualEffectView) { self.cancelButtonVisualEffectView.layer.cornerRadius = self.sheetContentCornerRadius; self.cancelButtonVisualEffectView.clipsToBounds = YES; }
if (self.scrollWrapView) { self.scrollWrapView.layer.cornerRadius = self.sheetContentCornerRadius; self.scrollWrapView.clipsToBounds = YES; }
}
}
- (void)setAlertTextFieldFont:(UIFont *)alertTextFieldFont {
_alertTextFieldFont = alertTextFieldFont;
[self.textFields enumerateObjectsUsingBlock:^(QMUITextField * _Nonnull textField, NSUInteger idx, BOOL * _Nonnull stop) {
textField.font = alertTextFieldFont;
}];
}
- (void)setAlertTextFieldBorderColor:(UIColor *)alertTextFieldBorderColor {
_alertTextFieldBorderColor = alertTextFieldBorderColor;
[self.textFields enumerateObjectsUsingBlock:^(QMUITextField * _Nonnull textField, NSUInteger idx, BOOL * _Nonnull stop) {
textField.layer.borderColor = alertTextFieldBorderColor.CGColor;
}];
}
- (void)setAlertTextFieldTextColor:(UIColor *)alertTextFieldTextColor {
_alertTextFieldTextColor = alertTextFieldTextColor;
[self.textFields enumerateObjectsUsingBlock:^(QMUITextField * _Nonnull textField, NSUInteger idx, BOOL * _Nonnull stop) {
textField.textColor = alertTextFieldTextColor;
}];
}
- (void)setAlertTextFieldTextInsets:(UIEdgeInsets)alertTextFieldTextInsets {
_alertTextFieldTextInsets = alertTextFieldTextInsets;
[self.textFields enumerateObjectsUsingBlock:^(QMUITextField * _Nonnull textField, NSUInteger idx, BOOL * _Nonnull stop) {
textField.textInsets = alertTextFieldTextInsets;
}];
}
- (void)setAlertTextFieldMarginBlock:(UIEdgeInsets (^)(__kindof QMUIAlertController * _Nonnull, NSInteger))alertTextFieldMarginBlock {
_alertTextFieldMarginBlock = alertTextFieldMarginBlock;
if (self.isViewLoaded) {
[self.view setNeedsLayout];
}
}
- (void)setMainVisualEffectView:(UIView *)mainVisualEffectView {
if (!mainVisualEffectView) {
// 不允许为空
mainVisualEffectView = [[UIView alloc] init];
}
BOOL isValueChanged = _mainVisualEffectView != mainVisualEffectView;
if (isValueChanged) {
if ([_mainVisualEffectView isKindOfClass:[UIVisualEffectView class]]) {
[((UIVisualEffectView *)_mainVisualEffectView).contentView qmui_removeAllSubviews];
} else {
[_mainVisualEffectView qmui_removeAllSubviews];
}
[_mainVisualEffectView removeFromSuperview];
_mainVisualEffectView = nil;
}
_mainVisualEffectView = mainVisualEffectView;
if (isValueChanged) {
[self.scrollWrapView insertSubview:_mainVisualEffectView atIndex:0];
[self updateCornerRadius];
}
}
- (void)setCancelButtonVisualEffectView:(UIView *)cancelButtonVisualEffectView {
if (!cancelButtonVisualEffectView) {
// 不允许为空
cancelButtonVisualEffectView = [[UIView alloc] init];
}
BOOL isValueChanged = _cancelButtonVisualEffectView != cancelButtonVisualEffectView;
if (isValueChanged) {
if ([_cancelButtonVisualEffectView isKindOfClass:[UIVisualEffectView class]]) {
[((UIVisualEffectView *)_cancelButtonVisualEffectView).contentView qmui_removeAllSubviews];
} else {
[_cancelButtonVisualEffectView qmui_removeAllSubviews];
}
[_cancelButtonVisualEffectView removeFromSuperview];
_cancelButtonVisualEffectView = nil;
}
_cancelButtonVisualEffectView = cancelButtonVisualEffectView;
if (isValueChanged) {
[self.containerView addSubview:_cancelButtonVisualEffectView];
if (self.preferredStyle == QMUIAlertControllerStyleActionSheet && self.cancelAction && !self.cancelAction.button.superview) {
if ([_cancelButtonVisualEffectView isKindOfClass:[UIVisualEffectView class]]) {
UIVisualEffectView *effectView = (UIVisualEffectView *)_cancelButtonVisualEffectView;
[effectView.contentView addSubview:self.cancelAction.button];
} else {
[_cancelButtonVisualEffectView addSubview:self.cancelAction.button];
}
}
[self updateCornerRadius];
}
}
+ (nonnull instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(QMUIAlertControllerStyle)preferredStyle {
QMUIAlertController *alertController = [[self alloc] initWithTitle:title message:message preferredStyle:preferredStyle];
if (alertController) {
return alertController;
}
return nil;
}
- (nonnull instancetype)initWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(QMUIAlertControllerStyle)preferredStyle {
self = [self init];
if (self) {
self.preferredStyle = preferredStyle;
self.shouldRespondDimmingViewTouch = preferredStyle == QMUIAlertControllerStyleActionSheet;
self.alertActions = [[NSMutableArray alloc] init];
self.alertTextFields = [[NSMutableArray alloc] init];
self.destructiveActions = [[NSMutableArray alloc] init];
self.title = title;
self.message = message;
self.mainVisualEffectView = [[UIView alloc] init];
self.cancelButtonVisualEffectView = [[UIView alloc] init];
}
return self;
}
- (QMUIAlertControllerStyle)preferredStyle {
return PreferredValueForDeviceIncludingiPad(1, 0, 0, 0, 0) > 0 ? QMUIAlertControllerStyleAlert : _preferredStyle;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.dimmingView];
[self.view addSubview:self.containerView];
[self.containerView addSubview:self.scrollWrapView];
[self.scrollWrapView addSubview:self.headerScrollView];
[self.scrollWrapView addSubview:self.buttonScrollView];
[self.containerView.layer addSublayer:self.extendLayer];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
BOOL hasTitle = (self.titleLabel.text.length > 0 && !self.titleLabel.hidden);
BOOL hasMessage = (self.messageLabel.text.length > 0 && !self.messageLabel.hidden);
BOOL hasTextField = self.alertTextFields.count > 0;
BOOL hasCustomView = !!_customView;
BOOL shouldShowSeparatorAtTopOfButtonAtFirstLine = hasTitle || hasMessage || hasCustomView;
CGFloat contentOriginY = 0;
self.dimmingView.frame = self.view.bounds;
if (self.preferredStyle == QMUIAlertControllerStyleAlert) {
CGFloat contentPaddingLeft = self.alertHeaderInsets.left;
CGFloat contentPaddingRight = self.alertHeaderInsets.right;
CGFloat contentPaddingTop = (hasTitle || hasMessage || hasTextField || hasCustomView) ? self.alertHeaderInsets.top : 0;
CGFloat contentPaddingBottom = (hasTitle || hasMessage || hasTextField || hasCustomView) ? self.alertHeaderInsets.bottom : 0;
self.containerView.qmui_width = fmin(self.alertContentMaximumWidth, CGRectGetWidth(self.view.bounds) - UIEdgeInsetsGetHorizontalValue(self.alertContentMargin));
self.scrollWrapView.qmui_width = CGRectGetWidth(self.containerView.bounds);
self.headerScrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollWrapView.bounds), 0);
contentOriginY = contentPaddingTop;
// 标题和副标题布局
if (hasTitle) {
self.titleLabel.frame = CGRectFlatted(CGRectMake(contentPaddingLeft, contentOriginY, CGRectGetWidth(self.headerScrollView.bounds) - contentPaddingLeft - contentPaddingRight, QMUIViewSelfSizingHeight));
contentOriginY = CGRectGetMaxY(self.titleLabel.frame) + (hasMessage ? self.alertTitleMessageSpacing : contentPaddingBottom);
}
if (hasMessage) {
self.messageLabel.frame = CGRectFlatted(CGRectMake(contentPaddingLeft, contentOriginY, CGRectGetWidth(self.headerScrollView.bounds) - contentPaddingLeft - contentPaddingRight, QMUIViewSelfSizingHeight));
contentOriginY = CGRectGetMaxY(self.messageLabel.frame) + contentPaddingBottom;
}
// 输入框布局
if (hasTextField) {
for (int i = 0; i < self.alertTextFields.count; i++) {
UITextField *textField = self.alertTextFields[i];
CGRect textFieldFrame = CGRectMake(contentPaddingLeft, contentOriginY, CGRectGetWidth(self.headerScrollView.bounds) - contentPaddingLeft - contentPaddingRight, CGFLOAT_MAX);
CGSize textFieldSize = [textField sizeThatFits:textFieldFrame.size];
textFieldFrame = CGRectSetHeight(textFieldFrame, textFieldSize.height);
UIEdgeInsets margin = UIEdgeInsetsZero;
if (self.alertTextFieldMarginBlock) {
margin = self.alertTextFieldMarginBlock(self, i);
}
textFieldFrame = CGRectMake(CGRectGetMinX(textFieldFrame) + margin.left, CGRectGetMinY(textFieldFrame) + margin.top, CGRectGetWidth(textFieldFrame) - UIEdgeInsetsGetHorizontalValue(margin), CGRectGetHeight(textFieldFrame));
contentOriginY = CGRectGetMaxY(textFieldFrame) + margin.bottom - textField.layer.borderWidth;
textField.frame = textFieldFrame;
}
}
// 自定义view的布局 - 自动居中
if (hasCustomView) {
CGSize customViewSize = [_customView sizeThatFits:CGSizeMake(CGRectGetWidth(self.headerScrollView.bounds), CGFLOAT_MAX)];
_customView.frame = CGRectFlatted(CGRectMake((CGRectGetWidth(self.headerScrollView.bounds) - customViewSize.width) / 2, contentOriginY, customViewSize.width, customViewSize.height));
contentOriginY = CGRectGetMaxY(_customView.frame) + contentPaddingBottom;
}
// 内容scrollView的布局
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, contentOriginY);
self.headerScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.headerScrollView.bounds), contentOriginY);
contentOriginY = CGRectGetMaxY(self.headerScrollView.frame);
// 按钮布局
self.buttonScrollView.frame = CGRectMake(0, contentOriginY, CGRectGetWidth(self.containerView.bounds), 0);
contentOriginY = 0;
NSArray<QMUIAlertAction *> *newOrderActions = [self orderedAlertActions:self.alertActions];
if (newOrderActions.count > 0) {
BOOL verticalLayout = YES;
if (self.alertActions.count == 2) {
CGFloat halfWidth = CGRectGetWidth(self.buttonScrollView.bounds) / 2;
QMUIAlertAction *action1 = newOrderActions[0];
QMUIAlertAction *action2 = newOrderActions[1];
CGSize actionSize1 = [action1.button sizeThatFits:CGSizeMax];
CGSize actionSize2 = [action2.button sizeThatFits:CGSizeMax];
if (actionSize1.width < halfWidth && actionSize2.width < halfWidth) {
verticalLayout = NO;
}
}
if (!verticalLayout) {
// 对齐系统,先 add 的在右边,后 add 的在左边
QMUIAlertAction *leftAction = newOrderActions[1];
leftAction.button.frame = CGRectMake(0, contentOriginY, CGRectGetWidth(self.buttonScrollView.bounds) / 2, self.alertButtonHeight);
leftAction.button.qmui_borderPosition = QMUIViewBorderPositionRight;
QMUIAlertAction *rightAction = newOrderActions[0];
rightAction.button.frame = CGRectMake(CGRectGetMaxX(leftAction.button.frame), contentOriginY, CGRectGetWidth(self.buttonScrollView.bounds) / 2, self.alertButtonHeight);
if (shouldShowSeparatorAtTopOfButtonAtFirstLine) {
leftAction.button.qmui_borderPosition |= QMUIViewBorderPositionTop;
rightAction.button.qmui_borderPosition = QMUIViewBorderPositionTop;
}
contentOriginY = CGRectGetMaxY(leftAction.button.frame);
} else {
for (int i = 0; i < newOrderActions.count; i++) {
QMUIAlertAction *action = newOrderActions[i];
action.button.frame = CGRectMake(0, contentOriginY, CGRectGetWidth(self.containerView.bounds), self.alertButtonHeight);
if (i > 0 || shouldShowSeparatorAtTopOfButtonAtFirstLine) {
action.button.qmui_borderPosition = QMUIViewBorderPositionTop;
}
contentOriginY = CGRectGetMaxY(action.button.frame);
}
}
}
// 按钮scrollView的布局
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, contentOriginY);
self.buttonScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.buttonScrollView.bounds), contentOriginY);
// 容器最后布局
CGFloat contentHeight = CGRectGetHeight(self.headerScrollView.bounds) + CGRectGetHeight(self.buttonScrollView.bounds);
CGFloat screenSpaceHeight = CGRectGetHeight(self.view.bounds) - UIEdgeInsetsGetVerticalValue(SafeAreaInsetsConstantForDeviceWithNotch) - self.keyboardHeight;
if (contentHeight > screenSpaceHeight - 20) {
screenSpaceHeight -= 20;
CGFloat contentH = fmin(CGRectGetHeight(self.headerScrollView.bounds), screenSpaceHeight / 2);
CGFloat buttonH = fmin(CGRectGetHeight(self.buttonScrollView.bounds), screenSpaceHeight / 2);
if (contentH >= screenSpaceHeight / 2 && buttonH >= screenSpaceHeight / 2) {
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, screenSpaceHeight / 2);
self.buttonScrollView.frame = CGRectSetY(self.buttonScrollView.frame, CGRectGetMaxY(self.headerScrollView.frame));
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, screenSpaceHeight / 2);
} else if (contentH < screenSpaceHeight / 2) {
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, contentH);
self.buttonScrollView.frame = CGRectSetY(self.buttonScrollView.frame, CGRectGetMaxY(self.headerScrollView.frame));
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, screenSpaceHeight - contentH);
} else if (buttonH < screenSpaceHeight / 2) {
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, screenSpaceHeight - buttonH);
self.buttonScrollView.frame = CGRectSetY(self.buttonScrollView.frame, CGRectGetMaxY(self.headerScrollView.frame));
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, buttonH);
}
contentHeight = CGRectGetHeight(self.headerScrollView.bounds) + CGRectGetHeight(self.buttonScrollView.bounds);
screenSpaceHeight += 20;
}
self.scrollWrapView.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollWrapView.bounds), contentHeight);
self.mainVisualEffectView.frame = self.scrollWrapView.bounds;
self.containerView.qmui_frameApplyTransform = CGRectMake((CGRectGetWidth(self.view.bounds) - CGRectGetWidth(self.containerView.frame)) / 2, SafeAreaInsetsConstantForDeviceWithNotch.top + (screenSpaceHeight - contentHeight) / 2, CGRectGetWidth(self.containerView.frame), CGRectGetHeight(self.scrollWrapView.bounds));
}
else if (self.preferredStyle == QMUIAlertControllerStyleActionSheet) {
CGFloat contentPaddingLeft = self.alertHeaderInsets.left;
CGFloat contentPaddingRight = self.alertHeaderInsets.right;
CGFloat contentPaddingTop = (hasTitle || hasMessage || hasTextField) ? self.sheetHeaderInsets.top : 0;
CGFloat contentPaddingBottom = (hasTitle || hasMessage || hasTextField) ? self.sheetHeaderInsets.bottom : 0;
self.containerView.qmui_width = fmin(self.sheetContentMaximumWidth, CGRectGetWidth(self.view.bounds) - UIEdgeInsetsGetHorizontalValue(self.sheetContentMargin));
self.scrollWrapView.qmui_width = CGRectGetWidth(self.containerView.bounds);
self.headerScrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.containerView.bounds), 0);
contentOriginY = contentPaddingTop;
// 标题和副标题布局
if (hasTitle) {
self.titleLabel.frame = CGRectFlatted(CGRectMake(contentPaddingLeft, contentOriginY, CGRectGetWidth(self.headerScrollView.bounds) - contentPaddingLeft - contentPaddingRight, QMUIViewSelfSizingHeight));
contentOriginY = CGRectGetMaxY(self.titleLabel.frame) + (hasMessage ? self.sheetTitleMessageSpacing : contentPaddingBottom);
}
if (hasMessage) {
self.messageLabel.frame = CGRectFlatted(CGRectMake(contentPaddingLeft, contentOriginY, CGRectGetWidth(self.headerScrollView.bounds) - contentPaddingLeft - contentPaddingRight, QMUIViewSelfSizingHeight));
contentOriginY = CGRectGetMaxY(self.messageLabel.frame) + contentPaddingBottom;
}
// 自定义view的布局 - 自动居中
if (hasCustomView) {
CGSize customViewSize = [_customView sizeThatFits:CGSizeMake(CGRectGetWidth(self.headerScrollView.bounds), CGFLOAT_MAX)];
_customView.frame = CGRectFlatted(CGRectMake((CGRectGetWidth(self.headerScrollView.bounds) - customViewSize.width) / 2, contentOriginY, customViewSize.width, customViewSize.height));
contentOriginY = CGRectGetMaxY(_customView.frame) + contentPaddingBottom;
}
// 内容scrollView布局
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, contentOriginY);
self.headerScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.headerScrollView.bounds), contentOriginY);
contentOriginY = CGRectGetMaxY(self.headerScrollView.frame);
// 按钮的布局
self.buttonScrollView.frame = CGRectMake(0, contentOriginY, CGRectGetWidth(self.containerView.bounds), 0);
NSArray<QMUIAlertAction *> *newOrderActions = [self orderedAlertActions:self.alertActions];
if (self.sheetButtonColumnCount > 1) {
// 如果是多列,则为了布局,补齐 item 个数
NSMutableArray<QMUIAlertAction *> *fixedActions = [newOrderActions mutableCopy];
[fixedActions removeObject:self.cancelAction];
if (fmodf(fixedActions.count, self.sheetButtonColumnCount) != 0) {
NSInteger increment = self.sheetButtonColumnCount - fmodf(fixedActions.count, self.sheetButtonColumnCount);
for (NSInteger i = 0; i < increment; i++) {
QMUIAlertAction *action = [[QMUIAlertAction alloc] init];
action.title = @"";
action.style = QMUIAlertActionStyleDefault;
action.handler = nil;
[self.buttonScrollView addSubview:action.button];
[fixedActions addObject:action];
}
[fixedActions addObject:self.cancelAction];
newOrderActions = [fixedActions copy];
}
}
CGFloat columnCount = self.sheetButtonColumnCount;
CGFloat alertActionsWidth = CGRectGetWidth(self.buttonScrollView.bounds) / columnCount;
CGFloat alertActionsLayoutX = 0;
CGFloat alertActionsLayoutY = 0;
contentOriginY = 0;
if (self.alertActions.count > 0) {
for (int i = 0; i < newOrderActions.count; i++) {
QMUIAlertAction *action = newOrderActions[i];
if (action.style == QMUIAlertActionStyleCancel && i == newOrderActions.count - 1) {
continue;
} else {
BOOL isFirstLine = floor(i / columnCount) == 0;
BOOL isLastColumn = fmod(i + 1, columnCount) == 0;
BOOL shouldShowSeparatorAtTop = !isFirstLine || shouldShowSeparatorAtTopOfButtonAtFirstLine;
BOOL shouldShowSeparatorAtRight = !isLastColumn;// 单列时全都不用显示右分隔线,多列时最后一列不用显示右分隔线
action.button.frame = CGRectMake(alertActionsLayoutX, alertActionsLayoutY, alertActionsWidth, self.sheetButtonHeight);
if (isLastColumn) {
alertActionsLayoutX = 0;
alertActionsLayoutY = CGRectGetMaxY(action.button.frame);
} else {
alertActionsLayoutX += alertActionsWidth;
}
contentOriginY = MAX(contentOriginY, CGRectGetMaxY(action.button.frame));
if (shouldShowSeparatorAtTop) {
action.button.qmui_borderPosition |= QMUIViewBorderPositionTop;
}
if (shouldShowSeparatorAtRight) {
action.button.qmui_borderPosition |= QMUIViewBorderPositionRight;
}
}
}
}
// 按钮scrollView布局
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, contentOriginY);
self.buttonScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.buttonScrollView.bounds), contentOriginY);
// 容器最终布局
self.scrollWrapView.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollWrapView.bounds), CGRectGetMaxY(self.buttonScrollView.frame));
self.mainVisualEffectView.frame = self.scrollWrapView.bounds;
contentOriginY = CGRectGetMaxY(self.scrollWrapView.frame) + self.sheetCancelButtonMarginTop;
if (self.cancelAction) {
self.cancelButtonVisualEffectView.frame = CGRectMake(0, contentOriginY, CGRectGetWidth(self.containerView.bounds), self.sheetButtonHeight);
self.cancelAction.button.frame = self.cancelButtonVisualEffectView.bounds;
contentOriginY = CGRectGetMaxY(self.cancelButtonVisualEffectView.frame);
}
// 把上下的margin都加上用于跟整个屏幕的高度做比较
CGFloat contentHeight = contentOriginY + UIEdgeInsetsGetVerticalValue(self.sheetContentMargin);
CGFloat screenSpaceHeight = CGRectGetHeight(self.view.bounds) - SafeAreaInsetsConstantForDeviceWithNotch.top - (self.isExtendBottomLayout ? 0 : SafeAreaInsetsConstantForDeviceWithNotch.bottom);
if (contentHeight > screenSpaceHeight) {
CGFloat cancelButtonAreaHeight = (self.cancelAction ? (CGRectGetHeight(self.cancelAction.button.bounds) + self.sheetCancelButtonMarginTop) : 0);
screenSpaceHeight = screenSpaceHeight - cancelButtonAreaHeight - UIEdgeInsetsGetVerticalValue(self.sheetContentMargin);
CGFloat contentH = MIN(CGRectGetHeight(self.headerScrollView.bounds), screenSpaceHeight / 2);
CGFloat buttonH = MIN(CGRectGetHeight(self.buttonScrollView.bounds), screenSpaceHeight / 2);
if (contentH >= screenSpaceHeight / 2 && buttonH >= screenSpaceHeight / 2) {
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, screenSpaceHeight / 2);
self.buttonScrollView.frame = CGRectSetY(self.buttonScrollView.frame, CGRectGetMaxY(self.headerScrollView.frame));
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, screenSpaceHeight / 2);
} else if (contentH < screenSpaceHeight / 2) {
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, contentH);
self.buttonScrollView.frame = CGRectSetY(self.buttonScrollView.frame, CGRectGetMaxY(self.headerScrollView.frame));
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, screenSpaceHeight - contentH);
} else if (buttonH < screenSpaceHeight / 2) {
self.headerScrollView.frame = CGRectSetHeight(self.headerScrollView.frame, screenSpaceHeight - buttonH);
self.buttonScrollView.frame = CGRectSetY(self.buttonScrollView.frame, CGRectGetMaxY(self.headerScrollView.frame));
self.buttonScrollView.frame = CGRectSetHeight(self.buttonScrollView.frame, buttonH);
}
self.scrollWrapView.frame = CGRectSetHeight(self.scrollWrapView.frame, CGRectGetHeight(self.headerScrollView.bounds) + CGRectGetHeight(self.buttonScrollView.bounds));
if (self.cancelAction) {
self.cancelButtonVisualEffectView.frame = CGRectSetY(self.cancelButtonVisualEffectView.frame, CGRectGetMaxY(self.scrollWrapView.frame) + self.sheetCancelButtonMarginTop);
}
contentHeight = CGRectGetHeight(self.headerScrollView.bounds) + CGRectGetHeight(self.buttonScrollView.bounds) + cancelButtonAreaHeight + self.sheetContentMargin.bottom;
screenSpaceHeight += (cancelButtonAreaHeight + UIEdgeInsetsGetVerticalValue(self.sheetContentMargin));
} else {
// 如果小于屏幕高度,则把顶部的top减掉
contentHeight -= self.sheetContentMargin.top;
}
self.containerView.qmui_frameApplyTransform = CGRectMake((CGRectGetWidth(self.view.bounds) - CGRectGetWidth(self.containerView.frame)) / 2, SafeAreaInsetsConstantForDeviceWithNotch.top + screenSpaceHeight - contentHeight, CGRectGetWidth(self.containerView.frame), contentHeight + (self.isExtendBottomLayout ? SafeAreaInsetsConstantForDeviceWithNotch.bottom : 0));
self.extendLayer.frame = CGRectFlatMake(0, CGRectGetHeight(self.containerView.bounds) - SafeAreaInsetsConstantForDeviceWithNotch.bottom - 1, CGRectGetWidth(self.containerView.bounds), SafeAreaInsetsConstantForDeviceWithNotch.bottom + 1);
}
}
- (NSArray<QMUIAlertAction *> *)orderedAlertActions:(NSArray<QMUIAlertAction *> *)actions {
NSMutableArray<QMUIAlertAction *> *newActions = [[NSMutableArray alloc] init];
// 按照用户addAction的先后顺序来排序
if (self.orderActionsByAddedOrdered) {
[newActions addObjectsFromArray:self.alertActions];
// 取消按钮不参与排序,所以先移除,在最后再重新添加
if (self.cancelAction) {
[newActions removeObject:self.cancelAction];
}
} else {
for (QMUIAlertAction *action in self.alertActions) {
if (action.style != QMUIAlertActionStyleCancel && action.style != QMUIAlertActionStyleDestructive) {
[newActions addObject:action];
}
}
for (QMUIAlertAction *action in self.destructiveActions) {
[newActions addObject:action];
}
}
if (self.cancelAction) {
[newActions addObject:self.cancelAction];
}
return newActions;
}
- (void)initModalPresentationController {
_modalPresentationViewController = [[QMUIModalPresentationViewController alloc] init];
self.modalPresentationViewController.delegate = self;
self.modalPresentationViewController.maximumContentViewWidth = CGFLOAT_MAX;
self.modalPresentationViewController.contentViewMargins = UIEdgeInsetsZero;
self.modalPresentationViewController.dimmingView = nil;
self.modalPresentationViewController.contentViewController = self;
[self customModalPresentationControllerAnimation];
}
- (void)customModalPresentationControllerAnimation {
__weak __typeof(self)weakSelf = self;
self.modalPresentationViewController.layoutBlock = ^(CGRect containerBounds, CGFloat keyboardHeight, CGRect contentViewDefaultFrame) {
weakSelf.view.frame = CGRectMake(0, 0, CGRectGetWidth(containerBounds), CGRectGetHeight(containerBounds));
weakSelf.keyboardHeight = keyboardHeight;
[weakSelf.view setNeedsLayout];
};
self.modalPresentationViewController.showingAnimation = ^(UIView *dimmingView, CGRect containerBounds, CGFloat keyboardHeight, CGRect contentViewFrame, void(^completion)(BOOL finished)) {
if (self.preferredStyle == QMUIAlertControllerStyleAlert) {
weakSelf.containerView.alpha = 0;
weakSelf.containerView.layer.transform = CATransform3DMakeScale(1.2, 1.2, 1.0);
[UIView animateWithDuration:0.25f delay:0 options:QMUIViewAnimationOptionsCurveOut animations:^{
weakSelf.dimmingView.alpha = 1;
weakSelf.containerView.alpha = 1;
weakSelf.containerView.layer.transform = CATransform3DMakeScale(1.0, 1.0, 1.0);
} completion:^(BOOL finished) {
if (completion) {
completion(finished);
}
}];
} else if (self.preferredStyle == QMUIAlertControllerStyleActionSheet) {
weakSelf.containerView.layer.transform = CATransform3DMakeTranslation(0, CGRectGetHeight(weakSelf.view.bounds) - CGRectGetMinY(weakSelf.containerView.frame), 0);
[UIView animateWithDuration:0.25f delay:0 options:QMUIViewAnimationOptionsCurveOut animations:^{
weakSelf.dimmingView.alpha = 1;
weakSelf.containerView.layer.transform = CATransform3DIdentity;
} completion:^(BOOL finished) {
if (completion) {
completion(finished);
}
}];
}
};
self.modalPresentationViewController.hidingAnimation = ^(UIView *dimmingView, CGRect containerBounds, CGFloat keyboardHeight, void(^completion)(BOOL finished)) {
if (self.preferredStyle == QMUIAlertControllerStyleAlert) {
[UIView animateWithDuration:0.25f delay:0 options:QMUIViewAnimationOptionsCurveOut animations:^{
weakSelf.dimmingView.alpha = 0;
weakSelf.containerView.alpha = 0;
} completion:^(BOOL finished) {
weakSelf.containerView.alpha = 1;
if (completion) {
completion(finished);
}
}];
} else if (self.preferredStyle == QMUIAlertControllerStyleActionSheet) {
[UIView animateWithDuration:0.25f delay:0 options:QMUIViewAnimationOptionsCurveOut animations:^{
weakSelf.dimmingView.alpha = 0;
weakSelf.containerView.layer.transform = CATransform3DMakeTranslation(0, CGRectGetHeight(weakSelf.view.bounds) - CGRectGetMinY(weakSelf.containerView.frame), 0);
} completion:^(BOOL finished) {
if (completion) {
completion(finished);
}
}];
}
};
}
- (void)showWithAnimated:(BOOL)animated {
[self showInWindow:nil animated:animated];
}
- (void)showInWindow:(nullable UIWindow *)window animated:(BOOL)animated {
if (self.willShow || self.showing) {
return;
}
self.willShow = YES;
if (self.alertTextFields.count > 0) {
[self.alertTextFields.firstObject becomeFirstResponder];
}
if (_needsUpdateAction) {
[self updateAction];
}
if (_needsUpdateTitle) {
[self updateTitleLabel];
}
if (_needsUpdateMessage) {
[self updateMessageLabel];
}
[self initModalPresentationController];
if ([self.delegate respondsToSelector:@selector(willShowAlertController:)]) {
[self.delegate willShowAlertController:self];
}
__weak __typeof(self)weakSelf = self;
[self.modalPresentationViewController showInWindow:window animated:animated completion:^(BOOL finished) {
weakSelf.dimmingView.alpha = 1;
weakSelf.willShow = NO;
weakSelf.showing = YES;
if (weakSelf.isNeedsHideAfterAlertShowed) {
[weakSelf hideWithAnimated:weakSelf.isAnimatedForHideAfterAlertShowed];
weakSelf.isNeedsHideAfterAlertShowed = NO;
weakSelf.isAnimatedForHideAfterAlertShowed = NO;
}
if ([weakSelf.delegate respondsToSelector:@selector(didShowAlertController:)]) {
[weakSelf.delegate didShowAlertController:weakSelf];
}
}];
// 增加alertController计数
alertControllerCount++;
}
- (void)hideWithAnimated:(BOOL)animated {
[self hideWithAnimated:animated completion:NULL];
}
- (void)hideWithAnimated:(BOOL)animated completion:(void (^)(void))completion {
if ([self.delegate respondsToSelector:@selector(shouldHideAlertController:)] && ![self.delegate shouldHideAlertController:self]) {
return;
}
if (!self.showing) {
if (self.willShow) {
self.isNeedsHideAfterAlertShowed = YES;
self.isAnimatedForHideAfterAlertShowed = animated;
}
return;
}
if ([self.delegate respondsToSelector:@selector(willHideAlertController:)]) {
[self.delegate willHideAlertController:self];
}
__weak __typeof(self)weakSelf = self;
[self.modalPresentationViewController hideWithAnimated:animated completion:^(BOOL finished) {
weakSelf.modalPresentationViewController = nil;
weakSelf.willShow = NO;
weakSelf.showing = NO;
weakSelf.dimmingView.alpha = 0;
if (self.preferredStyle == QMUIAlertControllerStyleAlert) {
weakSelf.containerView.alpha = 0;
} else {
weakSelf.containerView.layer.transform = CATransform3DMakeTranslation(0, CGRectGetHeight(weakSelf.view.bounds) - CGRectGetMinY(weakSelf.containerView.frame), 0);
}
if ([weakSelf.delegate respondsToSelector:@selector(didHideAlertController:)]) {
[weakSelf.delegate didHideAlertController:weakSelf];
}
if (completion) completion();
}];
// 减少alertController计数
alertControllerCount--;
}
- (void)addAction:(nonnull QMUIAlertAction *)action {
if (action.style == QMUIAlertActionStyleCancel && self.cancelAction) {
[NSException raise:@"QMUIAlertController使用错误" format:@"同一个alertController不可以同时添加两个cancel按钮"];
}
if (action.style == QMUIAlertActionStyleCancel) {
self.cancelAction = action;
}
if (action.style == QMUIAlertActionStyleDestructive) {
[self.destructiveActions addObject:action];
}
// 只有ActionSheet的取消按钮不参与滚动
if (self.preferredStyle == QMUIAlertControllerStyleActionSheet && action.style == QMUIAlertActionStyleCancel) {
if (!self.cancelButtonVisualEffectView.superview) {
[self.containerView addSubview:self.cancelButtonVisualEffectView];
}