Skip to content

Commit

Permalink
Work around the issue with sizing of views with many labels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aleh committed May 1, 2024
1 parent 491ffa8 commit 8885523
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MMMTestCase.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
17 changes: 12 additions & 5 deletions Sources/MMMTestCaseObjC/MMMTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 8885523

Please sign in to comment.