Skip to content

Commit

Permalink
Added X and Y coordinates to the current API which will change the ba…
Browse files Browse the repository at this point in the history
…dge position.

You can set the badge position for your default badge by using     tabBar.badgeOffset = CGPoint(x: 10, y: 10)

PiperOrigin-RevId: 683056798
  • Loading branch information
Nobody authored and material-automation committed Oct 7, 2024
1 parent f692bd8 commit bedb68e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
8 changes: 8 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ __attribute__((objc_subclassing_restricted)) @interface MDCTabBarView : UIScroll
@property(nonnull, nonatomic, copy)
UIColor *rippleColor __deprecated_msg("Enable disableRippleBehavior instead.");

/**
Offset to shift the badge from its default location.
Positive x values move the badge toward the trailing edge, and positive y values move the badge
downward. All badges in the tab bar use the same offset.
*/
@property(nonatomic) CGPoint badgeOffset;

@end

#if MDC_AVAILABLE_SDK_IOS(13_0) && !TARGET_OS_TV
Expand Down
10 changes: 10 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ - (void)setItems:(NSArray<UITabBarItem *> *)items {

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

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

- (void)setBadgeOffset:(CGPoint)badgeOffset {
_badgeOffset = badgeOffset;
for (UIView *itemView in self.itemViews) {
if ([itemView isKindOfClass:[MDCTabBarViewItemView class]]) {
((MDCTabBarViewItemView *)itemView).badgeOffset = badgeOffset;
}
}
}

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

Expand Down
10 changes: 9 additions & 1 deletion components/Tabs/src/TabBarView/private/MDCTabBarViewItemView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#import "MDCBadgeAppearance.h"
#import "MDCBadgeView.h"
#import <MaterialComponents/MaterialRipple.h>
#import "MaterialRipple.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -103,6 +103,14 @@ NS_ASSUME_NONNULL_BEGIN
MDCRippleTouchController *rippleTouchController __deprecated_msg(
"Enable disableRippleBehavior instead.");

/**
Offset to shift the badge from its default location.
Positive x values move the badge toward the trailing edge, and positive y values move the badge
downward.
*/
@property(nonatomic) CGPoint badgeOffset;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ - (CGPoint)badgeCenterFromFrame:(CGRect)frame isRTL:(BOOL)isRTL {
: (CGRectGetMaxX(frame) + kBadgeXOffset) - halfBadgeWidth;
xCenter -= _badge.appearance.borderWidth / 2;

return CGPointMake(xCenter, badgeCenterY);
return CGPointMake(xCenter + self.badgeOffset.x, badgeCenterY + self.badgeOffset.y);
}

- (CGPoint)centerForOnlyTitleFromFrame:(CGRect)frame isRTL:(BOOL)isRTL {
Expand All @@ -138,7 +138,7 @@ - (CGPoint)centerForOnlyTitleFromFrame:(CGRect)frame isRTL:(BOOL)isRTL {
CGFloat centerY = CGRectGetMidY(frame);
CGFloat centerX = isRTL ? CGRectGetMinX(frame) - halfBadgeWidth - kBadgeXOffset
: CGRectGetMaxX(frame) + halfBadgeWidth + kBadgeXOffset;
return CGPointMake(centerX, centerY);
return CGPointMake(centerX + self.badgeOffset.x, centerY + self.badgeOffset.y);
}

#pragma mark - UIView
Expand Down

0 comments on commit bedb68e

Please sign in to comment.