Skip to content

Commit

Permalink
[Tabs] Correct MDCTabBarView behavior for contentInset. (#8512)
Browse files Browse the repository at this point in the history
When `contentInset` is non-zero, MDCTabBarView was misplacing the tabs views.
This PR corrects the issue by making the following changes:

*  Clustered Fixed tabs now set the `contentSize` to fill the entire safe area
   bounds.
*  Justified tabs tabs now expand the `contentSize` to fill at least the entire safe
   area bounds.
*  Right-aligned tabs position tabs views only within the content area and no
   longer depend on the min-X position of the bounds.
*  Rendering the divider line at the end of `layoutSubviews` to handle cases
   where scrolling adjusted the X-offset of the bounds.

Fixes #8236
  • Loading branch information
Robert Moore committed Sep 27, 2019
1 parent 2420eba commit 86b5971
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
28 changes: 21 additions & 7 deletions components/Tabs/src/TabBarView/MDCTabBarView.m
Expand Up @@ -580,9 +580,6 @@ - (void)layoutSubviews {
}
}

self.bottomDividerView.frame =
CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMaxY(self.bounds) - kBottomDividerHeight,
CGRectGetWidth(self.bounds), kBottomDividerHeight);
self.contentSize = [self calculatedContentSize];
[self updateSelectionIndicatorToIndex:[self.items indexOfObject:self.selectedItem]];

Expand All @@ -598,6 +595,11 @@ - (void)layoutSubviews {
}
[self scrollUntilSelectedItemIsVisibleWithoutAnimation];
}
// It's possible that after scrolling the minX of bounds could have changed. Positioning it last
// ensures that its frame matches the displayed content bounds.
self.bottomDividerView.frame =
CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMaxY(self.bounds) - kBottomDividerHeight,
CGRectGetWidth(self.bounds), kBottomDividerHeight);
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
Expand Down Expand Up @@ -695,8 +697,7 @@ - (void)layoutSubviewsForFixedClusteredLayout:(MDCTabBarViewLayoutStyle)layoutSt
// Right-aligned
if ((isRTL && layoutStyle == MDCTabBarViewLayoutStyleFixedClusteredLeading) ||
(!isRTL && layoutStyle == MDCTabBarViewLayoutStyleFixedClusteredTrailing)) {
itemViewOriginX = CGRectGetMinX([self availableBoundsForSubviewLayout]) +
(contentSize.width - totalRequiredWidth);
itemViewOriginX = (contentSize.width - totalRequiredWidth);
}
// Centered
else if (layoutStyle == MDCTabBarViewLayoutStyleFixedClusteredCentered) {
Expand Down Expand Up @@ -760,15 +761,28 @@ - (CGSize)intrinsicContentSize {
}
}

/**
The content size of the tabs in their current layout style.
For @c FixedJustified: The content size is the maximum of the bounds within the safe area or the
intrinsic size of the tabs when all tabs have the widest tab's width.
For @c FixedClustered*: The bounds within the safe area. This ensures they are positionined
accurately within the content area.
For @c Scrollable: The intrinsic size size of the tabs.
*/
- (CGSize)calculatedContentSize {
switch ([self effectiveLayoutStyle]) {
case MDCTabBarViewLayoutStyleFixed: {
return [self intrinsicContentSizeForJustifiedLayout];
CGSize intrinsicContentSize = [self intrinsicContentSizeForJustifiedLayout];
CGSize boundsSize = CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));

return CGSizeMake(MAX(boundsSize.width, intrinsicContentSize.width),
MAX(boundsSize.height, intrinsicContentSize.height));
}
case MDCTabBarViewLayoutStyleFixedClusteredCentered:
case MDCTabBarViewLayoutStyleFixedClusteredTrailing:
case MDCTabBarViewLayoutStyleFixedClusteredLeading: {
return [self intrinsicContentSizeForClusteredLayout];
return [self availableSizeForSubviewLayout];
}
case MDCTabBarViewLayoutStyleScrollable: {
return [self intrinsicContentSizeForScrollableLayout];
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 86b5971

Please sign in to comment.