Skip to content

Commit 7883c8f

Browse files
committed
fix(bottom-navigation): iOS TabStrip. color not applied #464
1 parent 35bc6a0 commit 7883c8f

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed

src/bottom-navigation/index.ios.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import { TabStrip } from '@nativescript-community/ui-material-core-tabs/tab-stri
66
import { TabStripItem } from '@nativescript-community/ui-material-core-tabs/tab-strip-item';
77
// Types
88
// Requires
9-
import { CSSType, Color, CoreTypes, Device, Font, Frame, IOSHelper, ImageSource, ImageAsset, Property, Utils, View, booleanConverter } from '@nativescript/core';
9+
import { CSSType, Color, CoreTypes, Device, Font, Frame, IOSHelper, ImageAsset, ImageSource, Property, Utils, View, booleanConverter } from '@nativescript/core';
1010
import { getTransformedText } from '@nativescript/core/ui/text-base';
11-
import { iOSNativeHelper } from '@nativescript/core/utils';
11+
import { SDK_VERSION } from '@nativescript/core/utils';
1212
export { TabContentItem, TabStrip, TabStripItem };
1313

1414
// TODO:
1515
// import { profile } from "../../profiling";
1616

1717
const maxTabsCount = 5;
18-
const majorVersion = iOSNativeHelper.MajorVersion;
1918
const isPhone = Device.deviceType === 'Phone';
2019

2120
@NativeClass
@@ -29,8 +28,11 @@ class MDTabBarControllerImpl extends UITabBarController {
2928
return handler;
3029
}
3130

32-
// TODO
33-
// @profile
31+
public viewDidLoad() {
32+
super.viewDidLoad();
33+
this.tabBar.backgroundColor = new Color('#fff').ios;
34+
}
35+
3436
public viewWillAppear(animated: boolean): void {
3537
super.viewWillAppear(animated);
3638
const owner = this._owner.get();
@@ -48,8 +50,6 @@ class MDTabBarControllerImpl extends UITabBarController {
4850
}
4951
}
5052

51-
// TODO
52-
// @profile
5353
public viewDidDisappear(animated: boolean): void {
5454
super.viewDidDisappear(animated);
5555

@@ -80,7 +80,7 @@ class MDTabBarControllerImpl extends UITabBarController {
8080
public traitCollectionDidChange(previousTraitCollection: UITraitCollection): void {
8181
super.traitCollectionDidChange(previousTraitCollection);
8282

83-
if (majorVersion >= 13) {
83+
if (SDK_VERSION >= 13) {
8484
const owner = this._owner.get();
8585
if (
8686
owner &&
@@ -224,7 +224,7 @@ function updateBackgroundPositions(tabStrip: TabStrip, tabStripItem: TabStripIte
224224
} else {
225225
// always default to at least a solid white background as fallback
226226
// building with Xcode 13 causes bgView with no background to be fully transparent unless a css background-color is set - this allows original default behavior to work as it always did
227-
bgView.backgroundColor = new Color('#fff').ios;
227+
bgView.backgroundColor = null;
228228
}
229229
}
230230

@@ -237,7 +237,7 @@ function updateTitleAndIconPositions(tabStripItem: TabStripItem, tabBarItem: UIT
237237
// For iOS 11 icon is above the text *only* on phones in portrait mode.
238238
const orientation = controller.interfaceOrientation;
239239
const isPortrait = orientation !== UIInterfaceOrientation.LandscapeLeft && orientation !== UIInterfaceOrientation.LandscapeRight;
240-
const isIconAboveTitle = majorVersion < 11 || (isPhone && isPortrait);
240+
const isIconAboveTitle = SDK_VERSION < 11 || (isPhone && isPortrait);
241241

242242
if (!tabStripItem.iconSource) {
243243
if (isIconAboveTitle) {
@@ -392,7 +392,7 @@ export class BottomNavigation extends TabNavigationBase {
392392
}
393393

394394
public setTabBarBackgroundColor(value: UIColor | Color): void {
395-
this.viewController.tabBar.barTintColor = value instanceof Color ? value.ios : value;
395+
this.viewController.tabBar.backgroundColor = this.viewController.tabBar.barTintColor = value instanceof Color ? value.ios : value;
396396
this.updateAllItemsColors();
397397
}
398398

@@ -840,7 +840,7 @@ export class BottomNavigation extends TabNavigationBase {
840840
// to fix the above issue we are applying the selected fix only for the case, when there is no background set
841841
// in that case we have the following known issue:
842842
// // we will set the color to all unselected items, so you won't be able to set different colors for the different not selected items
843-
if (!this.viewController.tabBar.barTintColor && attributes[UITextAttributeTextColor] && majorVersion > 9) {
843+
if (!this.viewController.tabBar.barTintColor && attributes[UITextAttributeTextColor] && SDK_VERSION > 9) {
844844
this.viewController.tabBar.unselectedItemTintColor = attributes[UITextAttributeTextColor];
845845
}
846846
}

src/core-tabs/tab-navigation/index.android.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ function initializeNativeClasses() {
119119

120120
@NativeClass
121121
class FragmentPagerAdapter extends androidx.viewpager2.adapter.FragmentStateAdapter {
122-
constructor(public owner: WeakRef<TabNavigation>, fragmentActivity: androidx.fragment.app.FragmentActivity) {
122+
constructor(
123+
public owner: WeakRef<TabNavigation>,
124+
fragmentActivity: androidx.fragment.app.FragmentActivity
125+
) {
123126
super(fragmentActivity);
124127
return global.__native(this);
125128
}

src/core-tabs/tab-navigation/index.ios.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ export abstract class TabNavigation<
906906
});
907907
}
908908

909-
public setTabBarRippleColor(value: Color, alpha?:number) {
909+
public setTabBarRippleColor(value: Color, alpha?: number) {
910910
this.mRippleColor = value;
911911
this.viewController.tabBar.rippleColor = getRippleColor(value, alpha);
912912
}

src/core-tabs/tab-strip-item/index.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,12 @@ export class TabStripItem extends View implements TabStripItemDefinition, AddChi
258258
}
259259

260260
[backgroundColorProperty.getDefault](): Color {
261-
const parent = this.parent as TabStrip;
262-
const tabStripParent = parent && (parent.parent as TabNavigationBase);
263-
264-
return tabStripParent && tabStripParent.getTabBarBackgroundColor();
261+
const tabStripParent = (this.parent as TabStrip)?.parent as TabNavigationBase;
262+
return tabStripParent?.getTabBarBackgroundColor();
265263
}
266264
[backgroundColorProperty.setNative](value: Color) {
267-
const parent = this.parent as TabStrip;
268-
const tabStripParent = parent && (parent.parent as TabNavigationBase);
269-
270-
return tabStripParent && tabStripParent.setTabBarItemBackgroundColor(this, value);
265+
const tabStripParent = (this.parent as TabStrip)?.parent as TabNavigationBase;
266+
return tabStripParent?.setTabBarItemBackgroundColor(this, value);
271267
}
272268

273269
[textTransformProperty.getDefault](): CoreTypes.TextTransformType {

src/core-tabs/tab-strip/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,15 @@ export class TabStrip extends View implements TabStripDefinition, AddChildFromBu
9292
[backgroundColorProperty.getDefault](): Color {
9393
const parent = this.parent as TabNavigationBase;
9494

95-
return parent && parent.getTabBarBackgroundColor();
95+
return parent?.getTabBarBackgroundColor();
96+
// always default to at least a solid white background as fallback
97+
// building with Xcode 13 causes bgView with no background to be fully transparent unless a css background-color is set - this allows original default behavior to work as it always did
98+
// bgView.backgroundColor = new Color('#fff').ios;;
9699
}
97100
[backgroundColorProperty.setNative](value: Color) {
98101
const parent = this.parent as TabNavigationBase;
99102

100-
return parent && parent.setTabBarBackgroundColor(value);
103+
return parent?.setTabBarBackgroundColor(value);
101104
}
102105

103106
[backgroundInternalProperty.getDefault](): any {

src/tabs/index.ios.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class UIPageViewControllerImpl extends UIPageViewController {
266266
}
267267
}
268268

269-
export class Tabs extends TabNavigation<UIPageViewControllerImpl> {
269+
export class Tabs extends TabNavigation {
270270
protected createViewController() {
271271
return UIPageViewControllerImpl.initWithOwner(new WeakRef(this));
272272
}

0 commit comments

Comments
 (0)