34
34
35
35
NS_ASSUME_NONNULL_BEGIN
36
36
37
+ const int MAX_LAYOUT_PASSES = 10 ;
38
+
37
39
// The Bundle for string resources.
38
40
static NSString *const kMaterialDialogsBundle = @" MaterialDialogs.bundle" ;
39
41
@@ -105,6 +107,13 @@ @interface MDCAlertController () <UITextViewDelegate>
105
107
@property (nonatomic , nonnull , strong ) MDCAlertActionManager *actionManager;
106
108
@property (nonatomic , nullable , strong ) UIView *titleIconView;
107
109
110
+ /* *
111
+ This counter caps the maximum number of layout passes that can be done in a single layout cycle.
112
+
113
+ This variable is added as a direct fix for b/345505157.
114
+ */
115
+ @property (nonatomic ) int layoutPassCounter;
116
+
108
117
- (nonnull instancetype )initWithTitle : (nullable NSString *)title
109
118
message : (nullable NSString *)message ;
110
119
@@ -173,6 +182,7 @@ - (nonnull instancetype)initWithTitle:(nullable NSString *)title {
173
182
if (self) {
174
183
_transitionController = [[MDCDialogTransitionController alloc ] init ];
175
184
185
+ _layoutPassCounter = 0 ;
176
186
_alertTitle = [title copy ];
177
187
_titleAlignment = NSTextAlignmentNatural;
178
188
_messageAlignment = NSTextAlignmentNatural;
@@ -726,6 +736,12 @@ - (void)viewDidDisappear:(BOOL)animated {
726
736
}
727
737
728
738
- (void )viewDidLayoutSubviews {
739
+ // Increments the counter to account for an additional layout pass.
740
+ self.layoutPassCounter += 1 ;
741
+ // Abort if the layout pass counter is too high.
742
+ if (self.layoutPassCounter > MAX_LAYOUT_PASSES) {
743
+ return ;
744
+ }
729
745
// Recalculate preferredContentSize and potentially the view frame.
730
746
BOOL boundsSizeChanged =
731
747
!CGSizeEqualToSize (CGRectStandardize (self.view .bounds ).size , _previousLayoutSize);
@@ -752,6 +768,8 @@ - (void)viewDidLayoutSubviews {
752
768
753
769
- (void )viewWillLayoutSubviews {
754
770
[super viewWillLayoutSubviews ];
771
+ // Resets counter as this function is called at the beginning of a new layout cycle.
772
+ self.layoutPassCounter = 0 ;
755
773
756
774
// Recalculate preferredSize, which is based on width available, if the viewSize has changed.
757
775
if (CGRectGetWidth (self.view .bounds ) != _previousLayoutSize.width ||
0 commit comments