From 888552341f851278bcee9cac854d089aad6d2233 Mon Sep 17 00:00:00 2001 From: Aleh Dzenisiuk Date: Wed, 1 May 2024 18:44:37 +0200 Subject: [PATCH] Work around the issue with sizing of views with many labels In cases when multiple multi-line labels depend on each other Auto Layout was sometimes reporting way larger size, which appeared to be related to multiple layout passes needed in such cases. Having the view being sized in a window appears to be helping with this issue. --- MMMTestCase.podspec | 2 +- Sources/MMMTestCaseObjC/MMMTestCase.m | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/MMMTestCase.podspec b/MMMTestCase.podspec index b79a8fa..3025c4a 100644 --- a/MMMTestCase.podspec +++ b/MMMTestCase.podspec @@ -6,7 +6,7 @@ Pod::Spec.new do |s| s.name = "MMMTestCase" - s.version = "1.10.0" + s.version = "1.11.0" s.summary = "Our helpers for FBTestCase and XCTestCase" s.description = s.summary s.homepage = "https://github.com/mediamonks/#{s.name}" diff --git a/Sources/MMMTestCaseObjC/MMMTestCase.m b/Sources/MMMTestCaseObjC/MMMTestCase.m index 3f5eb82..740628b 100644 --- a/Sources/MMMTestCaseObjC/MMMTestCase.m +++ b/Sources/MMMTestCaseObjC/MMMTestCase.m @@ -532,9 +532,13 @@ - (void)verifyView:(UIView *)view fitSize:(CGSize)fitSize identifier:(NSString * MMMTestCaseContainer *outerContainer = [[MMMTestCaseContainer alloc] init]; outerContainer.backgroundColor = backgroundColor ?: [UIColor whiteColor]; - // To ensure any small updates scheduled on the current run loop (or dispatch queue attached to it) are procesed before we begin measuring things. + // To ensure any small updates scheduled on the current run loop (or dispatch queue attached to it) are processed before we begin measuring things. [self pumpRunLoopABit]; + // Let's remember the superview this view was a part of, to restore this afterwards. + UIView *originalSuperview = view.superview; + NSInteger originalIndex = originalSuperview ? [view.superview.subviews indexOfObjectIdenticalTo:view] : NSNotFound; + CGSize size; if (view.translatesAutoresizingMaskIntoConstraints) { @@ -546,6 +550,13 @@ - (void)verifyView:(UIView *)view fitSize:(CGSize)fitSize identifier:(NSString * } else { + // Let's have a window to make sure sizing works properly with layouts involving many + // multi-line labels depending on each other. + UIWindow *window = [[UIWindow alloc] init]; + [window setHidden:NO]; + [window setWindowLevel:-1]; + [window addSubview:view]; + [view updateConstraintsIfNeeded]; size = [view @@ -558,10 +569,6 @@ - (void)verifyView:(UIView *)view fitSize:(CGSize)fitSize identifier:(NSString * ]; } - // Let's remember the superview this view was a part of, to restore this afterwards. - UIView *originalSuperview = view.superview; - NSInteger originalIndex = originalSuperview ? [view.superview.subviews indexOfObjectIdenticalTo:view] : NSNotFound; - [outerContainer setChildView:view size:size]; CGSize containerSize = [outerContainer sizeThatFits:CGSizeZero]; outerContainer.frame = CGRectMake(0, 0, containerSize.width, containerSize.height);