Skip to content

Commit

Permalink
Improve sizing of views requiring multi-pass layout
Browse files Browse the repository at this point in the history
  • Loading branch information
aleh committed May 24, 2024
1 parent 8885523 commit a84dcc8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 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.11.0"
s.version = "1.12.0"
s.summary = "Our helpers for FBTestCase and XCTestCase"
s.description = s.summary
s.homepage = "https://github.com/mediamonks/#{s.name}"
Expand Down
32 changes: 23 additions & 9 deletions Sources/MMMTestCaseObjC/MMMTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -557,16 +557,30 @@ - (void)verifyView:(UIView *)view fitSize:(CGSize)fitSize identifier:(NSString *
[window setWindowLevel:-1];
[window addSubview:view];

[view updateConstraintsIfNeeded];

size = [view
systemLayoutSizeFittingSize:CGSizeMake(
fitSize.width <= 0 ? 0 : fitSize.width,
fitSize.height <= 0 ? 0 : fitSize.height
)
withHorizontalFittingPriority:(fitSize.width <= 0) ? UILayoutPriorityFittingSizeLevel : UILayoutPriorityRequired
verticalFittingPriority:(fitSize.height <= 0) ? UILayoutPriorityFittingSizeLevel : UILayoutPriorityRequired
// A replacement for `systemLayoutSizeFittingSize` that should be able to handle cases where multi-pass layout is required.
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint
constraintWithItem:view attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute
multiplier:1 constant:fitSize.width <= 0 ? 0 : fitSize.width
];
widthConstraint.priority = (fitSize.width <= 0) ? UILayoutPriorityFittingSizeLevel : UILayoutPriorityRequired;
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint
constraintWithItem:view attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute
multiplier:1 constant:fitSize.height <= 0 ? 0 : fitSize.height
];
heightConstraint.priority = (fitSize.height <= 0) ? UILayoutPriorityFittingSizeLevel : UILayoutPriorityRequired;

[NSLayoutConstraint activateConstraints:@[widthConstraint, heightConstraint]];

// This should call those pending layoutIfNeeded, multiple times if needed.
[self pumpRunLoopABit];

[NSLayoutConstraint deactivateConstraints:@[widthConstraint, heightConstraint]];

size = view.bounds.size;
}

[outerContainer setChildView:view size:size];
Expand Down

0 comments on commit a84dcc8

Please sign in to comment.