Skip to content

Commit

Permalink
Merge pull request #613 from grahammendick/tabbar-appearance
Browse files Browse the repository at this point in the history
  • Loading branch information
grahammendick authored Jul 10, 2022
2 parents e8532d3 + c9f34a5 commit 18c2b2a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 29 deletions.
5 changes: 4 additions & 1 deletion NavigationReactNative/src/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TabBar extends React.Component<any, any> {
bottomTabs = bottomTabs != null ? bottomTabs : primary;
var tabBarItems = React.Children.toArray(children).filter(child => !!child);
var titleOnly = !tabBarItems.find(({props}: any) => props.title && props.image);
var {fontFamily, fontWeight, fontStyle, fontSize} = (tabBarItems[0] as any)?.props || {};
var {fontFamily, fontWeight, fontStyle, fontSize, badgeColor} = (tabBarItems[0] as any)?.props || {};
var tabViewHeight = !primary ? (titleOnly ? 48 : 72) : 56
tabViewHeight = Platform.OS === 'android' ? tabViewHeight : 28;
var TabBarPager = (Platform.OS === 'ios' || !I18nManager.isRTL) ? NVTabBarPager : NVTabBarPagerRTL;
Expand Down Expand Up @@ -89,7 +89,10 @@ class TabBar extends React.Component<any, any> {
barTintColor={barTintColor}
selectedTintColor={selectedTintColor}
unselectedTintColor={unselectedTintColor}
badgeColor={badgeColor}
scrollsToTop={scrollsToTop}
fontFamily={fontFamily} fontWeight={fontWeight}
fontStyle={fontStyle} fontSize={fontSize}
mostRecentEventCount={this.state.mostRecentEventCount}
style={styles.tabBar}>
<BackButton onPress={() => this.changeTab(0)} />
Expand Down
6 changes: 6 additions & 0 deletions NavigationReactNative/src/TabBarNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ type NativeProps = $ReadOnly<{|
barTintColor: ColorValue,
selectedTintColor: ColorValue,
unselectedTintColor: ColorValue,
badgeColor: ColorValue,
scrollsToTop: boolean,
fontFamily: string,
fontWeight: string,
fontStyle: string,
fontSize?: WithDefault<Float, -1>,
testID: string,
mostRecentEventCount: Int32,
onTabSelected: DirectEventHandler<$ReadOnly<{|
tab: Int32,
Expand Down
4 changes: 4 additions & 0 deletions NavigationReactNative/src/ios/NVTabBarComponentView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ NS_ASSUME_NONNULL_BEGIN
@interface NVTabBarComponentView : RCTViewComponentView <UITabBarControllerDelegate>

@property (nonatomic, assign) BOOL scrollsToTop;
@property (nonatomic, copy) NSString *fontFamily;
@property (nonatomic, copy) NSString *fontWeight;
@property (nonatomic, copy) NSString *fontStyle;
@property (nonatomic, copy) NSNumber *fontSize;
@property (nonatomic, assign) NSInteger mostRecentEventCount;

@end
Expand Down
35 changes: 30 additions & 5 deletions NavigationReactNative/src/ios/NVTabBarComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "RCTFabricComponentsPlugins.h"
#import <React/UIView+React.h>
#import <React/RCTConversions.h>
#import <React/RCTFont.h>
#import <React/RCTI18nUtil.h>
#import <React/RCTScrollViewComponentView.h>

Expand Down Expand Up @@ -61,19 +62,43 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
[_tabBarController.tabBar setTintColor: selectedTintColor];
if ([_tabBarController.tabBar unselectedItemTintColor] != unselectedTintColor)
[_tabBarController.tabBar setUnselectedItemTintColor: unselectedTintColor];
if (@available(iOS 15.0, *)) {
UITabBarAppearance *appearance = nil;
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
[appearance configureWithDefaultBackground];
if (barTintColor) {
appearance = [UITabBarAppearance new];
[appearance configureWithDefaultBackground];
if (CGColorGetAlpha(barTintColor.CGColor) == 0)
[appearance configureWithTransparentBackground];
if (CGColorGetAlpha(barTintColor.CGColor) == 1)
[appearance configureWithOpaqueBackground];
[appearance setBackgroundColor:barTintColor];
}
_fontFamily = [[NSString alloc] initWithUTF8String: newViewProps.fontFamily.c_str()];
_fontFamily = _fontFamily.length ? _fontFamily : nil;
_fontWeight = [[NSString alloc] initWithUTF8String: newViewProps.fontWeight.c_str()];
_fontWeight = _fontWeight.length ? _fontWeight : nil;
_fontStyle = [[NSString alloc] initWithUTF8String: newViewProps.fontStyle.c_str()];
_fontStyle = _fontStyle.length ? _fontStyle : nil;
_fontSize = @(newViewProps.fontSize);
_fontSize = [_fontSize intValue] >= 0 ? _fontSize : nil;
UIFont *baseFont = !self.fontFamily ? [UIFont systemFontOfSize:UIFont.labelFontSize] : nil;
NSNumber *size = !self.fontSize ? @10 : self.fontSize;
NSString *weight = !self.fontWeight ? @"500" : self.fontWeight;
UIFont *font = [RCTFont updateFont:baseFont withFamily:self.fontFamily size:size weight:weight style:self.fontStyle variant:nil scaleMultiplier:1];
NSMutableDictionary *attributes = [NSMutableDictionary new];
if (self.fontFamily || self.fontWeight || self.fontStyle || self.fontSize) {
attributes[NSFontAttributeName] = font;
}
UITabBarItemAppearance *itemAppearance = [UITabBarItemAppearance new];
UIColor *badgeColor = RCTUIColorFromSharedColor(newViewProps.badgeColor);
[itemAppearance.normal setBadgeBackgroundColor:badgeColor];
[itemAppearance.selected setBadgeBackgroundColor:badgeColor];
[itemAppearance.normal setTitleTextAttributes:attributes];
[itemAppearance.selected setTitleTextAttributes:attributes];
appearance.stackedLayoutAppearance = itemAppearance;
appearance.compactInlineLayoutAppearance = itemAppearance;
_tabBarController.tabBar.standardAppearance = appearance;
_tabBarController.tabBar.scrollEdgeAppearance = appearance;
if (@available(iOS 15.0, *))
_tabBarController.tabBar.scrollEdgeAppearance = appearance;
} else {
[_tabBarController.tabBar setBarTintColor:barTintColor];
}
Expand Down
4 changes: 1 addition & 3 deletions NavigationReactNative/src/ios/NVTabBarItemComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
NSString *badge = [[NSString alloc] initWithUTF8String: newViewProps.badge.c_str()];
if (self.tab.badgeValue != badge)
self.tab.badgeValue = !!badge.length ? badge : nil;
UIColor *badgeColor = RCTUIColorFromSharedColor(newViewProps.badgeColor);
if (self.tab.badgeColor != badgeColor)
self.tab.badgeColor = UIColor.greenColor;
self.tab.badgeColor = RCTUIColorFromSharedColor(newViewProps.badgeColor);
NSString *uri = [[NSString alloc] initWithUTF8String:newViewProps.image.uri.c_str()];
if (![uri length]) {
_image = nil;
Expand Down
5 changes: 5 additions & 0 deletions NavigationReactNative/src/ios/NVTabBarManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(barTintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(selectedTintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(unselectedTintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(badgeColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(scrollsToTop, BOOL)
RCT_EXPORT_VIEW_PROPERTY(fontFamily, NSString)
RCT_EXPORT_VIEW_PROPERTY(fontWeight, NSString)
RCT_EXPORT_VIEW_PROPERTY(fontStyle, NSString)
RCT_EXPORT_VIEW_PROPERTY(fontSize, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(onTabSelected, RCTDirectEventBlock)

Expand Down
6 changes: 6 additions & 0 deletions NavigationReactNative/src/ios/NVTabBarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
@interface NVTabBarView : UIView <UITabBarControllerDelegate>

@property (nonatomic, assign) NSInteger tabCount;
@property (nonatomic, copy) UIColor *barTintColor;
@property (nonatomic, copy) UIColor *badgeColor;
@property (nonatomic, assign) BOOL scrollsToTop;
@property (nonatomic, copy) NSString *fontFamily;
@property (nonatomic, copy) NSString *fontWeight;
@property (nonatomic, copy) NSString *fontStyle;
@property (nonatomic, copy) NSNumber *fontSize;
@property (nonatomic, assign) NSInteger mostRecentEventCount;
@property (nonatomic, copy) RCTDirectEventBlock onTabSelected;

Expand Down
52 changes: 32 additions & 20 deletions NavigationReactNative/src/ios/NVTabBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import <UIKit/UIKit.h>
#import <React/UIView+React.h>
#import <React/RCTFont.h>
#import <React/RCTI18nUtil.h>
#import <React/RCTScrollView.h>

Expand Down Expand Up @@ -34,26 +35,6 @@ - (void)setSelectedTab:(NSInteger)selectedTab
}
}

- (void)setBarTintColor:(UIColor *)barTintColor
{
if (@available(iOS 15.0, *)) {
UITabBarAppearance *appearance = nil;
if (barTintColor) {
appearance = [UITabBarAppearance new];
[appearance configureWithDefaultBackground];
if (CGColorGetAlpha(barTintColor.CGColor) == 0)
[appearance configureWithTransparentBackground];
if (CGColorGetAlpha(barTintColor.CGColor) == 1)
[appearance configureWithOpaqueBackground];
[appearance setBackgroundColor:barTintColor];
}
_tabBarController.tabBar.standardAppearance = appearance;
_tabBarController.tabBar.scrollEdgeAppearance = appearance;
} else {
[_tabBarController.tabBar setBarTintColor:barTintColor];
}
}

- (void)setSelectedTintColor:(UIColor *)selectedTintColor
{
[_tabBarController.tabBar setTintColor: selectedTintColor];
Expand Down Expand Up @@ -96,6 +77,37 @@ - (void)didSetProps:(NSArray<NSString *> *)changedProps
}
[self selectTab];
}
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
[appearance configureWithDefaultBackground];
if (_barTintColor) {
if (CGColorGetAlpha(_barTintColor.CGColor) == 0)
[appearance configureWithTransparentBackground];
if (CGColorGetAlpha(_barTintColor.CGColor) == 1)
[appearance configureWithOpaqueBackground];
[appearance setBackgroundColor:_barTintColor];
}
UIFont *baseFont = !self.fontFamily ? [UIFont systemFontOfSize:UIFont.labelFontSize] : nil;
NSNumber *size = !self.fontSize ? @10 : self.fontSize;
NSString *weight = !self.fontWeight ? @"500" : self.fontWeight;
UIFont *font = [RCTFont updateFont:baseFont withFamily:self.fontFamily size:size weight:weight style:self.fontStyle variant:nil scaleMultiplier:1];
NSMutableDictionary *attributes = [NSMutableDictionary new];
if (self.fontFamily || self.fontWeight || self.fontStyle || self.fontSize) {
attributes[NSFontAttributeName] = font;
}
UITabBarItemAppearance *itemAppearance = [UITabBarItemAppearance new];
[itemAppearance.normal setBadgeBackgroundColor:_badgeColor];
[itemAppearance.selected setBadgeBackgroundColor:_badgeColor];
[itemAppearance.normal setTitleTextAttributes:attributes];
[itemAppearance.selected setTitleTextAttributes:attributes];
appearance.stackedLayoutAppearance = itemAppearance;
appearance.compactInlineLayoutAppearance = itemAppearance;
_tabBarController.tabBar.standardAppearance = appearance;
if (@available(iOS 15.0, *))
_tabBarController.tabBar.scrollEdgeAppearance = appearance;
} else {
[_tabBarController.tabBar setBarTintColor:_barTintColor];
}
}

- (void)didMoveToWindow
Expand Down

7 comments on commit 18c2b2a

@gezquinndesign
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance you could also add these options to SearchBar please?

@grahammendick
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gezquinndesign Sure, but I'm back on the Fabric migration so might not get round to it for a while. Would you be interested in working on the PR?

@gezquinndesign
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very new to React Native but I can give it a go. I'll use this as a guide.

@grahammendick
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gezquinndesign That's awesome. If you get stuck or just have questions, please reach out and I'll do my best to help you 👍

@grahammendick
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gezquinndesign How you getting on? Have you tried this yet? If not, I could maybe give it a go if you still need it. I've finished the Fabric migration work so I might have some free time. Let me know either way

@grahammendick
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gezquinndesign
Copy link

@gezquinndesign gezquinndesign commented on 18c2b2a Jul 9, 2023 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.