Skip to content

Commit 5836cd4

Browse files
[List] Fix self sizing layout in iOS 13 (material-components#7536)
List cell self sizing wasn't working in iOS 13. This PR fixes it. Here's an iOS 13 before photo: ![Simulator Screen Shot - iPhone Xʀ - 2019-06-04 at 10 20 50](https://user-images.githubusercontent.com/8020010/58887161-1a4c8080-86b3-11e9-960d-f5f73be17349.png) Here's an iOS 13 after photo: ![Simulator Screen Shot - iPhone Xʀ - 2019-06-04 at 10 21 27](https://user-images.githubusercontent.com/8020010/58887160-1a4c8080-86b3-11e9-8399-991551b794af.png) Here's an iOS 11 after photo: ![Simulator Screen Shot - iPhone 8 - 2019-06-04 at 10 25 35](https://user-images.githubusercontent.com/8020010/58887159-1a4c8080-86b3-11e9-80f6-6b23a0a7961c.png) Closes #7533.
1 parent 0563c90 commit 5836cd4

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

components/List/src/MDCSelfSizingStereoCell.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ - (void)setNeedsLayout {
142142
[super setNeedsLayout];
143143
}
144144

145+
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:
146+
(UICollectionViewLayoutAttributes *)layoutAttributes {
147+
UICollectionViewLayoutAttributes *attributes =
148+
[super preferredLayoutAttributesFittingAttributes:layoutAttributes];
149+
attributes.size = [self systemLayoutSizeFittingSize:layoutAttributes.size];
150+
return attributes;
151+
}
152+
145153
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize {
146154
MDCSelfSizingStereoCellLayout *layout = [self layoutForCellWidth:targetSize.width];
147155
return CGSizeMake(targetSize.width, layout.calculatedHeight);

components/List/tests/unit/MDCSelfSizingStereoCellTests.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,28 @@ - (void)testDoesNotAdjustFontForContentSizeCategoryWhenScaledFontIsUnavailableIs
153153
XCTAssertTrue([cell.detailLabel.font mdc_isSimplyEqual:originalDetailFont]);
154154
}
155155

156+
- (void)testSizingWithPreferredLayoutAttributesFittingAttributes {
157+
// Given
158+
NSString *oneLineString = @"Text spanning one line";
159+
NSString *twoLineString = @"Text spanning\ntwo lines";
160+
CGSize estimatedCellSize = CGSizeMake(100, 40);
161+
CGRect initialCellFrame = CGRectMake(0, 0, estimatedCellSize.width, estimatedCellSize.height);
162+
MDCSelfSizingStereoCell *cell = [[MDCSelfSizingStereoCell alloc] initWithFrame:initialCellFrame];
163+
cell.titleLabel.text = oneLineString;
164+
cell.detailLabel.text = oneLineString;
165+
UICollectionViewLayoutAttributes *attributes = [[UICollectionViewLayoutAttributes alloc] init];
166+
attributes.size = estimatedCellSize;
167+
attributes = [cell preferredLayoutAttributesFittingAttributes:attributes];
168+
CGSize initialAttributeSize = attributes.size;
169+
170+
// When
171+
cell.titleLabel.text = twoLineString;
172+
cell.detailLabel.text = twoLineString;
173+
[cell setNeedsLayout];
174+
attributes = [cell preferredLayoutAttributesFittingAttributes:attributes];
175+
176+
// Then
177+
XCTAssert(attributes.size.height > initialAttributeSize.height);
178+
}
179+
156180
@end

0 commit comments

Comments
 (0)