Skip to content

Commit

Permalink
[Tabs] Adjust sizeThatFits: behavior for TabBarView. (#7846)
Browse files Browse the repository at this point in the history
The `sizeThatFits:` beahvior of `MDCTabBarView` previously would return a size
large enough for Fixed tabs in a justified layout. If the provided `size` was
wider than necessary, that value would be returned.

A solution that works better for inclusion in MDCHeaderStackView, which uses
`sizeThatFits:` to determine the view's frame size.

Closes #7845
  • Loading branch information
Robert Moore committed Jul 10, 2019
1 parent 29e37b8 commit 7c7208f
Show file tree
Hide file tree
Showing 21 changed files with 198 additions and 21 deletions.
2 changes: 2 additions & 0 deletions MaterialComponentsBeta.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Pod::Spec.new do |mdc|
unit_tests.source_files = [
"components/#{extension.base_name.split('+')[0]}/tests/unit/#{extension.base_name.split('+')[1]}/*.{h,m,swift}",
]
unit_tests.dependency "MaterialComponents/AppBar"
unit_tests.dependency "MaterialComponents/HeaderStackView"
unit_tests.dependency "MaterialComponents/Typography"
end
end
Expand Down
6 changes: 4 additions & 2 deletions components/Tabs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ mdc_extension_objc_library(
name = "Theming",
deps = [
":Tabs",
"//components/schemes/Container",
"//components/schemes/Color",
"//components/schemes/Container",
"//components/schemes/Typography",
],
)
Expand Down Expand Up @@ -186,8 +186,8 @@ mdc_objc_library(
deps = [
":Tabs",
":Theming",
"//components/schemes/Container",
"//components/schemes/Color",
"//components/schemes/Container",
"//components/schemes/Typography",
],
)
Expand Down Expand Up @@ -225,6 +225,8 @@ mdc_snapshot_objc_library(
":Tabs",
":Theming",
":privateTabBarView",
"//components/AppBar",
"//components/HeaderStackView",
],
)

Expand Down
4 changes: 2 additions & 2 deletions components/Tabs/src/TabBarView/MDCTabBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ - (CGSize)intrinsicContentSizeForScrollableLayout {
}

- (CGSize)sizeThatFits:(CGSize)size {
CGSize intrinsicSize = self.intrinsicContentSize;
return CGSizeMake(MAX(intrinsicSize.width, size.width), intrinsicSize.height);
CGSize fitSize = [self intrinsicContentSizeForJustifiedLayout];
return CGSizeMake(size.width, fitSize.height);
}

#pragma mark - Helpers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2019-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "MaterialSnapshot.h"

#import "MDCTabBarView.h"
#import "MaterialAppBar.h"
#import "MaterialHeaderStackView.h"

/** Snapshot tests for MDCTabBarView when integrating with other components. */
@interface MDCTabBarViewIntegrationSnapshotTests : MDCSnapshotTestCase

/** The tab bar being tested and integrated. */
@property(nonatomic, strong) MDCTabBarView *tabBarView;

@end

@implementation MDCTabBarViewIntegrationSnapshotTests

- (void)setUp {
[super setUp];

// Uncomment below to recreate all the goldens (or add the following line to the specific
// test you wish to recreate the golden for).
// self.recordMode = YES;

self.tabBarView = [[MDCTabBarView alloc] init];

UIImage *typicalIcon = [[UIImage mdc_testImageOfSize:CGSizeMake(24, 24)
withStyle:MDCSnapshotTestImageStyleFramedX]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"One" image:typicalIcon tag:0];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Two" image:typicalIcon tag:1];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Three" image:typicalIcon tag:2];
self.tabBarView.items = @[ item1, item2, item3 ];
[self.tabBarView setSelectedItem:item3 animated:NO];
}

- (void)tearDown {
self.tabBarView = nil;

[super tearDown];
}

- (void)generateSnapshotAndVerifyForView:(UIView *)view {
UIView *snapshotView = [view mdc_addToBackgroundView];
[self snapshotVerifyView:snapshotView];
}

#pragma mark - MDCHeaderStackView Integration

- (void)testTabBarViewInHeaderStackViewBottomBarWithWidthLessThanIntrinsicContentSize {
// Given
MDCHeaderStackView *headerStackView = [[MDCHeaderStackView alloc] init];
headerStackView.backgroundColor = UIColor.orangeColor;
CGSize tabBarViewIntrinsicContentSize = self.tabBarView.intrinsicContentSize;

// When
headerStackView.bottomBar = self.tabBarView;
headerStackView.bounds = CGRectMake(0, 0, tabBarViewIntrinsicContentSize.width - 50,
tabBarViewIntrinsicContentSize.height + 50);
[headerStackView layoutIfNeeded];

// Then
[self generateSnapshotAndVerifyForView:headerStackView];
}

- (void)testTabBarViewInHeaderStackViewBottomBarWithWidthEqualToIntrinsicContentSize {
// Given
MDCHeaderStackView *headerStackView = [[MDCHeaderStackView alloc] init];
headerStackView.backgroundColor = UIColor.orangeColor;
CGSize tabBarViewIntrinsicContentSize = self.tabBarView.intrinsicContentSize;

// When
headerStackView.bottomBar = self.tabBarView;
headerStackView.bounds = CGRectMake(0, 0, tabBarViewIntrinsicContentSize.width,
tabBarViewIntrinsicContentSize.height + 50);
[headerStackView layoutIfNeeded];

// Then
[self generateSnapshotAndVerifyForView:headerStackView];
}

- (void)testTabBarViewInHeaderStackViewBottomBarWithWidthGreaterThanIntrinsicContentSize {
// Given
MDCHeaderStackView *headerStackView = [[MDCHeaderStackView alloc] init];
headerStackView.backgroundColor = UIColor.orangeColor;
CGSize tabBarViewIntrinsicContentSize = self.tabBarView.intrinsicContentSize;

// When
headerStackView.bottomBar = self.tabBarView;
headerStackView.bounds = CGRectMake(0, 0, tabBarViewIntrinsicContentSize.width + 50,
tabBarViewIntrinsicContentSize.height + 50);
[headerStackView layoutIfNeeded];

// Then
[self generateSnapshotAndVerifyForView:headerStackView];
}

#pragma mark - MDCAppBarViewController Integration

- (void)testTabBarViewInAppBarViewBottomBarWithWidthLessThanIntrinsicContentSize {
// Given
MDCAppBarViewController *appBarController = [[MDCAppBarViewController alloc] init];
appBarController.navigationBar.title = @"TabBarView";
appBarController.headerView.minimumHeight = 120;
appBarController.headerView.backgroundColor = UIColor.orangeColor;
CGSize tabBarViewIntrinsicContentSize = self.tabBarView.intrinsicContentSize;

// When
appBarController.headerStackView.bottomBar = self.tabBarView;
appBarController.view.bounds = CGRectMake(0, 0, tabBarViewIntrinsicContentSize.width - 50,
tabBarViewIntrinsicContentSize.height + 50);
[appBarController.view layoutIfNeeded];

// Then
[self generateSnapshotAndVerifyForView:appBarController.view];
}

- (void)testTabBarViewInAppBarViewBottomBarWithWidthEqualToIntrinsicContentSize {
// Given
MDCAppBarViewController *appBarController = [[MDCAppBarViewController alloc] init];
appBarController.navigationBar.title = @"TabBarView";
appBarController.headerView.minimumHeight = 120;
appBarController.headerView.backgroundColor = UIColor.orangeColor;
CGSize tabBarViewIntrinsicContentSize = self.tabBarView.intrinsicContentSize;

// When
appBarController.headerStackView.bottomBar = self.tabBarView;
appBarController.view.bounds = CGRectMake(0, 0, tabBarViewIntrinsicContentSize.width,
tabBarViewIntrinsicContentSize.height + 50);
[appBarController.view layoutIfNeeded];

// Then
[self generateSnapshotAndVerifyForView:appBarController.view];
}

- (void)testTabBarViewInAppBarViewBottomBarWithWidthGreaterThanIntrinsicContentSize {
// Given
MDCAppBarViewController *appBarController = [[MDCAppBarViewController alloc] init];
appBarController.navigationBar.title = @"TabBarView";
appBarController.headerView.minimumHeight = 120;
appBarController.headerView.backgroundColor = UIColor.orangeColor;
CGSize tabBarViewIntrinsicContentSize = self.tabBarView.intrinsicContentSize;

// When
appBarController.headerStackView.bottomBar = self.tabBarView;
appBarController.view.bounds = CGRectMake(0, 0, tabBarViewIntrinsicContentSize.width + 50,
tabBarViewIntrinsicContentSize.height + 50);
[appBarController.view layoutIfNeeded];

// Then
[self generateSnapshotAndVerifyForView:appBarController.view];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
/** The maximum width of a tab bar item. */
static const CGFloat kMaxItemWidth = 360;

static inline void SizeViewToIntrinsicContentSize(UIView *view) {
CGSize intrinsicContentSize = view.intrinsicContentSize;
view.bounds = CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height);
}

/** A custom view to place in an MDCTabBarView. */
@interface MDCTabBarViewSnapshotTestsCustomView : UIView <MDCTabBarViewCustomViewable>
/** A switch shown in the view. */
Expand Down Expand Up @@ -393,7 +398,7 @@ - (void)testRemovingSelectedItemUpdatesStyle {
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"One" image:self.typicalIcon1 tag:0];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Two" image:self.typicalIcon2 tag:1];
self.tabBarView.items = @[ item1, item2 ];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);
[self.tabBarView setSelectedItem:item1 animated:NO];
[self.tabBarView setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
[self.tabBarView setImageTintColor:UIColor.blackColor forState:UIControlStateNormal];
Expand Down Expand Up @@ -703,7 +708,7 @@ - (void)testSafeAreaTopAndLeftInsetsForJustifiedLayoutStyle {
// When
UIEdgeInsets safeAreaInsets = UIEdgeInsetsMake(16, 44, 0, 0);
superview.customSafeAreaInsets = safeAreaInsets;
CGSize fitSize = [self.tabBarView sizeThatFits:CGSizeZero];
CGSize fitSize = self.tabBarView.intrinsicContentSize;
fitSize = CGSizeMake(fitSize.width + safeAreaInsets.left, fitSize.height + safeAreaInsets.top);
self.tabBarView.bounds = CGRectMake(0, 0, fitSize.width, fitSize.height);
superview.bounds = CGRectMake(0, 0, CGRectGetWidth(self.tabBarView.bounds),
Expand All @@ -728,7 +733,7 @@ - (void)testSafeAreaRightAndBottomInsetsForJustifiedLayoutStyle {
// When
UIEdgeInsets safeAreaInsets = UIEdgeInsetsMake(0, 0, 16, 44);
superview.customSafeAreaInsets = safeAreaInsets;
CGSize fitSize = [self.tabBarView sizeThatFits:CGSizeZero];
CGSize fitSize = self.tabBarView.intrinsicContentSize;
fitSize =
CGSizeMake(fitSize.width + safeAreaInsets.right, fitSize.height + safeAreaInsets.bottom);
self.tabBarView.bounds = CGRectMake(0, 0, fitSize.width, fitSize.height);
Expand Down Expand Up @@ -817,7 +822,7 @@ - (void)testCustomInsetsTopAndRightRepositionsJustifiedViews {
// When
UIEdgeInsets contentInset = UIEdgeInsetsMake(15, 0, 0, 45);
self.tabBarView.contentInset = contentInset;
CGSize fitSize = [self.tabBarView sizeThatFits:CGSizeZero];
CGSize fitSize = self.tabBarView.intrinsicContentSize;
fitSize = CGSizeMake(fitSize.width + contentInset.right, fitSize.height + contentInset.top);
self.tabBarView.bounds = CGRectMake(0, 0, fitSize.width, fitSize.height);

Expand All @@ -837,7 +842,7 @@ - (void)testCustomInsetsBottomAndLeftRepositionsJustifiedViews {
// When
UIEdgeInsets contentInset = UIEdgeInsetsMake(0, 45, 15, 0);
self.tabBarView.contentInset = contentInset;
CGSize fitSize = [self.tabBarView sizeThatFits:CGSizeZero];
CGSize fitSize = self.tabBarView.intrinsicContentSize;
fitSize = CGSizeMake(fitSize.width + contentInset.left, fitSize.height + contentInset.bottom);
self.tabBarView.bounds = CGRectMake(0, 0, fitSize.width, fitSize.height);

Expand Down Expand Up @@ -894,7 +899,7 @@ - (void)testSafeAreaAndContentInsetCombineTopAndLeftForJustifiedLayoutStyle {
UIEdgeInsets contentInset = UIEdgeInsetsMake(5, 10, 0, 0);
superview.customSafeAreaInsets = safeAreaInsets;
self.tabBarView.contentInset = contentInset;
CGSize fitSize = [self.tabBarView sizeThatFits:CGSizeZero];
CGSize fitSize = self.tabBarView.intrinsicContentSize;
fitSize = CGSizeMake(fitSize.width + safeAreaInsets.left + contentInset.left,
fitSize.height + safeAreaInsets.top + contentInset.top);
self.tabBarView.bounds = CGRectMake(0, 0, fitSize.width, fitSize.height);
Expand Down Expand Up @@ -922,7 +927,7 @@ - (void)testSafeAreaAndContentInsetCombineBottomAndRightForJustifiedLayoutStyle
UIEdgeInsets contentInset = UIEdgeInsetsMake(0, 0, 5, 10);
superview.customSafeAreaInsets = safeAreaInsets;
self.tabBarView.contentInset = contentInset;
CGSize fitSize = [self.tabBarView sizeThatFits:CGSizeZero];
CGSize fitSize = self.tabBarView.intrinsicContentSize;
fitSize = CGSizeMake(fitSize.width + safeAreaInsets.right + contentInset.right,
fitSize.height + safeAreaInsets.bottom + contentInset.bottom);
self.tabBarView.bounds = CGRectMake(0, 0, fitSize.width, fitSize.height);
Expand Down Expand Up @@ -1050,7 +1055,7 @@ - (void)testSetTitleFontForExplicitItemStates {
// When
[self.tabBarView setTitleFont:[UIFont systemFontOfSize:8] forState:UIControlStateNormal];
[self.tabBarView setTitleFont:[UIFont systemFontOfSize:24] forState:UIControlStateSelected];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);

// Then
[self generateSnapshotAndVerifyForView:self.tabBarView];
Expand All @@ -1066,7 +1071,7 @@ - (void)testSetTitleFontForNormalStateAppliesToSelectedItem {

// When
[self.tabBarView setTitleFont:[UIFont systemFontOfSize:8] forState:UIControlStateNormal];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);

// Then
[self generateSnapshotAndVerifyForView:self.tabBarView];
Expand All @@ -1085,7 +1090,7 @@ - (void)testSetTitleFontExplicitlyToNilUsesDefaultFont {
// When
[self.tabBarView setTitleFont:nil forState:UIControlStateNormal];
[self.tabBarView setTitleFont:nil forState:UIControlStateSelected];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);

// Then
[self generateSnapshotAndVerifyForView:self.tabBarView];
Expand Down Expand Up @@ -1128,7 +1133,7 @@ - (void)testRippleColor {
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Three" image:self.typicalIcon3 tag:3];
self.tabBarView.items = @[ item1, item2, item3 ];
[self.tabBarView setSelectedItem:item2 animated:NO];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);

// When
self.tabBarView.rippleColor = [UIColor.orangeColor colorWithAlphaComponent:(CGFloat)0.25];
Expand All @@ -1145,7 +1150,7 @@ - (void)testCustomSelectionIndicatorStrokeColor {
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Three" image:self.typicalIcon3 tag:3];
self.tabBarView.items = @[ item1, item2, item3 ];
[self.tabBarView setSelectedItem:item2 animated:NO];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);

// When
self.tabBarView.selectionIndicatorStrokeColor = UIColor.purpleColor;
Expand All @@ -1161,7 +1166,7 @@ - (void)testSelectionIndicatorStrokeColorResetsToDefault {
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Three" image:self.typicalIcon3 tag:3];
self.tabBarView.items = @[ item1, item2, item3 ];
[self.tabBarView setSelectedItem:item2 animated:NO];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);
self.tabBarView.selectionIndicatorStrokeColor = UIColor.purpleColor;

// When
Expand All @@ -1178,7 +1183,7 @@ - (void)testBottomDividerColor {
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Three" image:self.typicalIcon3 tag:3];
self.tabBarView.items = @[ item1, item2, item3 ];
[self.tabBarView setSelectedItem:item2 animated:NO];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);
self.tabBarView.bottomDividerColor = UIColor.purpleColor;

// When
Expand All @@ -1201,7 +1206,7 @@ - (void)testCustomView {

// When
self.tabBarView.items = @[ item1, customViewItem, item3 ];
[self.tabBarView sizeToFit];
SizeViewToIntrinsicContentSize(self.tabBarView);

// Then
[self generateSnapshotAndVerifyForView:self.tabBarView];
Expand Down
4 changes: 2 additions & 2 deletions components/Tabs/tests/unit/TabBarView/MDCTabBarViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -679,15 +679,15 @@ - (void)testIntrinsicContentSizeDeterminedByJustifiedViewNotScrollableView {
XCTAssertGreaterThanOrEqual(actualIntrinsicContentSize.height, intrinsicSizeMinimalSize.height);
}

- (void)testSizeThatFitsExpandsToFitContent {
- (void)testSizeThatFitsFitsOnlyIncreasesHeightForTooSmallSize {
// Given
self.tabBarView.items = @[ self.itemA ];

// When
CGSize size = [self.tabBarView sizeThatFits:CGSizeZero];

// Then
XCTAssertGreaterThan(size.width, 0);
XCTAssertEqualWithAccuracy(size.width, 0, 0.001);
XCTAssertEqualWithAccuracy(size.height, kMinHeight, 0.001);
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
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 7c7208f

Please sign in to comment.