Skip to content

Commit

Permalink
#Tabs Add IconSize parameter to MDCTabBarView
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 620265575
  • Loading branch information
codeman7 authored and material-automation committed Mar 29, 2024
1 parent 69f2f02 commit 48cd681
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
9 changes: 9 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ __attribute__((objc_subclassing_restricted)) @interface MDCTabBarView : UIScroll
*/
@property(nonatomic) BOOL disableRippleBehavior;

/**
The size of the icons within the tab bar.
This property is not respected unless a value other than @c CGSizeZero is used.
@note Defaults to CGSizeZero.
*/
@property(nonatomic, assign) CGSize itemIconSize;

/**
The default appearance to be used for all item badges.
Expand Down
13 changes: 12 additions & 1 deletion components/Tabs/src/TabBarView/MDCTabBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ - (void)commonMDCTabBarViewInit {
_itemBadgeAppearance = [[MDCBadgeAppearance alloc] init];
_itemBadgeAppearance.textColor = UIColor.whiteColor;
_itemBadgeAppearance.font = [UIFont systemFontOfSize:kBadgeFontSize];

_itemIconSize = CGSizeZero;
_selectionIndicatorView = [[MDCTabBarViewIndicatorView alloc] init];
_selectionIndicatorView.translatesAutoresizingMaskIntoConstraints = NO;
_selectionIndicatorView.userInteractionEnabled = NO;
Expand Down Expand Up @@ -313,6 +313,7 @@ - (void)setItems:(NSArray<UITabBarItem *> *)items {
mdcItemView.rippleTouchController.rippleView.rippleColor = self.rippleColor;

mdcItemView.badgeAppearance = self.itemBadgeAppearance;
mdcItemView.iconSize = self.itemIconSize;

#if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
if (@available(iOS 13, *)) {
Expand Down Expand Up @@ -375,6 +376,16 @@ - (void)setItemBadgeAppearance:(MDCBadgeAppearance *)itemBadgeAppearance {
}
}

- (void)setItemIconSize:(CGSize)itemIconSize {
_itemIconSize = itemIconSize;

for (UIView *itemView in self.itemViews) {
if ([itemView isKindOfClass:[MDCTabBarViewItemView class]]) {
((MDCTabBarViewItemView *)itemView).iconSize = _itemIconSize;
}
}
}

- (void)setDisableRippleBehavior:(BOOL)disableRippleBehavior {
_disableRippleBehavior = disableRippleBehavior;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ NS_ASSUME_NONNULL_BEGIN
/** The image view to display the icon. */
@property(nonatomic, strong, nonnull) UIImageView *iconImageView;

/**
The size of the icon.
This property is not respected unless a value other than @c CGSizeZero is used.
@note Defaults to CGSizeZero.
*/
@property(nonatomic, assign) CGSize iconSize;

/** The label to display the title. */
@property(nonatomic, strong, nonnull) UILabel *titleLabel;

Expand Down
16 changes: 14 additions & 2 deletions components/Tabs/src/TabBarView/private/MDCTabBarViewItemView.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ - (void)commonMDCTabBarViewItemViewInit {
[self addSubview:_badge];
_badge.hidden = YES;
}

_iconSize = CGSizeZero;
}

- (CGPoint)badgeCenterFromFrame:(CGRect)frame isRTL:(BOOL)isRTL {
Expand Down Expand Up @@ -200,7 +202,9 @@ - (CGRect)iconImageViewFrameForImageOnlyLayout {
self.bounds, [self contentInsetsForItemViewStyle:MDCTabBarViewItemViewStyleImageOnly]);

CGSize contentSize = CGSizeMake(CGRectGetWidth(contentFrame), CGRectGetHeight(contentFrame));
CGSize imageIntrinsicContentSize = self.iconImageView.intrinsicContentSize;
CGSize imageIntrinsicContentSize = CGSizeEqualToSize(self.iconSize, CGSizeZero)
? self.iconImageView.intrinsicContentSize
: self.iconSize;
CGSize imageFinalSize = CGSizeMake(MIN(contentSize.width, imageIntrinsicContentSize.width),
MIN(contentSize.height, imageIntrinsicContentSize.height));
CGRect imageViewFrame = CGRectMake(CGRectGetMidX(contentFrame) - (imageFinalSize.width / 2),
Expand All @@ -220,7 +224,9 @@ - (void)layoutTitleLabelFrame:(CGRect *)titleLabelFrame
contentSize.width, contentSize.height - (kImageTitlePadding + labelSingleLineSize.height));

// Position the image, limiting it so that at least 1 line of text remains.
CGSize imageIntrinsicContentSize = self.iconImageView.intrinsicContentSize;
CGSize imageIntrinsicContentSize = CGSizeEqualToSize(self.iconSize, CGSizeZero)
? self.iconImageView.intrinsicContentSize
: self.iconSize;
CGSize imageFinalSize =
CGSizeMake(MIN(imageIntrinsicContentSize.width, availableIconSize.width),
MIN(imageIntrinsicContentSize.height, availableIconSize.height));
Expand Down Expand Up @@ -411,6 +417,12 @@ - (void)setBadgeColor:(nullable UIColor *)badgeColor {
[self commitBadgeAppearance];
}

- (void)setIconSize:(CGSize)iconSize {
_iconSize = iconSize;

[self setNeedsLayout];
}

#pragma mark - UIAccessibility

- (nullable NSString *)accessibilityLabel {
Expand Down

0 comments on commit 48cd681

Please sign in to comment.