From b069ff38f3f9e32507089f8599da78546da78dbd Mon Sep 17 00:00:00 2001 From: Vasil Chimev Date: Mon, 18 Nov 2019 13:39:00 +0200 Subject: [PATCH] fix(iOS): check for iOS 13 APIs (#8093) --- nativescript-core/application/application.ios.ts | 3 +-- .../ui/activity-indicator/activity-indicator.ios.ts | 2 +- .../ui/bottom-navigation/bottom-navigation.ios.ts | 4 +++- nativescript-core/ui/core/view/view.ios.ts | 4 +++- nativescript-core/ui/frame/frame.ios.ts | 4 +++- nativescript-core/ui/html-view/html-view.ios.ts | 2 +- nativescript-core/ui/page/page.ios.ts | 6 ++++-- nativescript-core/ui/tab-view/tab-view.ios.ts | 4 +++- nativescript-core/ui/tabs/tabs.ios.ts | 10 ++++++---- nativescript-core/ui/text-base/text-base.ios.ts | 4 ++-- nativescript-core/ui/text-view/text-view.ios.ts | 4 ++-- tests/app/ui/page/page-tests-common.ts | 2 +- 12 files changed, 30 insertions(+), 19 deletions(-) diff --git a/nativescript-core/application/application.ios.ts b/nativescript-core/application/application.ios.ts index 13efff29b..5a4191d56 100644 --- a/nativescript-core/application/application.ios.ts +++ b/nativescript-core/application/application.ios.ts @@ -89,7 +89,7 @@ class CADisplayLinkTarget extends NSObject { } class IOSApplication implements IOSApplicationDefinition { - private _backgroundColor = majorVersion <= 12 ? UIColor.whiteColor : UIColor.systemBackgroundColor; + private _backgroundColor = (majorVersion <= 12 || !UIColor.systemBackgroundColor) ? UIColor.whiteColor : UIColor.systemBackgroundColor; private _delegate: typeof UIApplicationDelegate; private _window: UIWindow; private _observers: Array; @@ -121,7 +121,6 @@ class IOSApplication implements IOSApplicationDefinition { } get systemAppearance(): "light" | "dark" | null { - // userInterfaceStyle is available on UITraitCollection since iOS 12. if (majorVersion <= 11) { return null; diff --git a/nativescript-core/ui/activity-indicator/activity-indicator.ios.ts b/nativescript-core/ui/activity-indicator/activity-indicator.ios.ts index 9ce1dfb51..153e45673 100644 --- a/nativescript-core/ui/activity-indicator/activity-indicator.ios.ts +++ b/nativescript-core/ui/activity-indicator/activity-indicator.ios.ts @@ -9,7 +9,7 @@ const majorVersion = ios.MajorVersion; export class ActivityIndicator extends ActivityIndicatorBase { nativeViewProtected: UIActivityIndicatorView; - private _activityIndicatorViewStyle = majorVersion <= 12 ? UIActivityIndicatorViewStyle.Gray : UIActivityIndicatorViewStyle.Medium; + private _activityIndicatorViewStyle = (majorVersion <= 12 || !UIActivityIndicatorViewStyle.Medium) ? UIActivityIndicatorViewStyle.Gray : UIActivityIndicatorViewStyle.Medium; createNativeView() { const viewStyle = this._activityIndicatorViewStyle; diff --git a/nativescript-core/ui/bottom-navigation/bottom-navigation.ios.ts b/nativescript-core/ui/bottom-navigation/bottom-navigation.ios.ts index b5e06d67a..f94154dfc 100644 --- a/nativescript-core/ui/bottom-navigation/bottom-navigation.ios.ts +++ b/nativescript-core/ui/bottom-navigation/bottom-navigation.ios.ts @@ -93,7 +93,9 @@ class UITabBarControllerImpl extends UITabBarController { if (majorVersion >= 13) { const owner = this._owner.get(); - if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { + if (owner && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: iosView.traitCollectionColorAppearanceChangedEvent, object: owner }); } } diff --git a/nativescript-core/ui/core/view/view.ios.ts b/nativescript-core/ui/core/view/view.ios.ts index 83c5e4ce2..30c58eb69 100644 --- a/nativescript-core/ui/core/view/view.ios.ts +++ b/nativescript-core/ui/core/view/view.ios.ts @@ -1012,7 +1012,9 @@ export namespace ios { if (majorVersion >= 13) { const owner = this.owner.get(); - if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { + if (owner && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: traitCollectionColorAppearanceChangedEvent, object: owner }); } } diff --git a/nativescript-core/ui/frame/frame.ios.ts b/nativescript-core/ui/frame/frame.ios.ts index 145754bfb..bd3b1e1e1 100644 --- a/nativescript-core/ui/frame/frame.ios.ts +++ b/nativescript-core/ui/frame/frame.ios.ts @@ -540,7 +540,9 @@ class UINavigationControllerImpl extends UINavigationController { if (majorVersion >= 13) { const owner = this._owner.get(); - if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { + if (owner && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: iosView.traitCollectionColorAppearanceChangedEvent, object: owner }); } } diff --git a/nativescript-core/ui/html-view/html-view.ios.ts b/nativescript-core/ui/html-view/html-view.ios.ts index 3252c3626..d5c1d5b6e 100644 --- a/nativescript-core/ui/html-view/html-view.ios.ts +++ b/nativescript-core/ui/html-view/html-view.ios.ts @@ -59,7 +59,7 @@ export class HtmlView extends HtmlViewBase { null ); - if (majorVersion >= 13) { + if (majorVersion >= 13 && UIColor.labelColor) { this.nativeViewProtected.textColor = UIColor.labelColor; } } diff --git a/nativescript-core/ui/page/page.ios.ts b/nativescript-core/ui/page/page.ios.ts index 4fd9a4fc3..d54cdf649 100644 --- a/nativescript-core/ui/page/page.ios.ts +++ b/nativescript-core/ui/page/page.ios.ts @@ -287,7 +287,9 @@ class UIViewControllerImpl extends UIViewController { if (majorVersion >= 13) { const owner = this._owner.get(); - if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { + if (owner && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: iosView.traitCollectionColorAppearanceChangedEvent, object: owner }); } } @@ -298,7 +300,7 @@ export class Page extends PageBase { nativeViewProtected: UIView; viewController: UIViewControllerImpl; - private _backgroundColor = majorVersion <= 12 ? UIColor.whiteColor : UIColor.systemBackgroundColor; + private _backgroundColor = (majorVersion <= 12 && !UIColor.systemBackgroundColor) ? UIColor.whiteColor : UIColor.systemBackgroundColor; private _ios: UIViewControllerImpl; public _presentedViewController: UIViewController; // used when our page present native viewController without going through our abstraction. diff --git a/nativescript-core/ui/tab-view/tab-view.ios.ts b/nativescript-core/ui/tab-view/tab-view.ios.ts index 02e00b24f..7d0cef33c 100644 --- a/nativescript-core/ui/tab-view/tab-view.ios.ts +++ b/nativescript-core/ui/tab-view/tab-view.ios.ts @@ -78,7 +78,9 @@ class UITabBarControllerImpl extends UITabBarController { if (majorVersion >= 13) { const owner = this._owner.get(); - if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { + if (owner && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: iosView.traitCollectionColorAppearanceChangedEvent, object: owner }); } } diff --git a/nativescript-core/ui/tabs/tabs.ios.ts b/nativescript-core/ui/tabs/tabs.ios.ts index d7d622371..3358e7d16 100644 --- a/nativescript-core/ui/tabs/tabs.ios.ts +++ b/nativescript-core/ui/tabs/tabs.ios.ts @@ -24,7 +24,7 @@ export * from "./tabs-common"; const majorVersion = iosUtils.MajorVersion; // Equivalent to dispatch_async(dispatch_get_main_queue(...)) call -const invokeOnRunLoop = (function() { +const invokeOnRunLoop = (function () { const runloop = CFRunLoopGetMain(); return (action: () => any) => { @@ -100,7 +100,7 @@ class UIPageViewControllerImpl extends UIPageViewController { tabBar.delegate = this.tabBarDelegate = MDCTabBarDelegateImpl.initWithOwner(new WeakRef(owner)); - if (majorVersion <= 12) { + if (majorVersion <= 12 || !UIColor.labelColor) { tabBar.tintColor = UIColor.blueColor; tabBar.barTintColor = UIColor.whiteColor; tabBar.setTitleColorForState(UIColor.blackColor, MDCTabBarItemState.Normal); @@ -216,7 +216,9 @@ class UIPageViewControllerImpl extends UIPageViewController { if (majorVersion >= 13) { const owner = this._owner.get(); - if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { + if (owner && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && + this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: iosView.traitCollectionColorAppearanceChangedEvent, object: owner }); } } @@ -1087,7 +1089,7 @@ export class Tabs extends TabsBase { // HACK: UIPageViewController fix; see https://stackoverflow.com/a/17330606 invokeOnRunLoop(() => this.viewController.setViewControllersDirectionAnimatedCompletion(controllers, navigationDirection, false, null)); } - + this._canSelectItem = true; this._setCanBeLoaded(value); this._loadUnloadTabItems(value); diff --git a/nativescript-core/ui/text-base/text-base.ios.ts b/nativescript-core/ui/text-base/text-base.ios.ts index 4665757c0..12cb12e30 100644 --- a/nativescript-core/ui/text-base/text-base.ios.ts +++ b/nativescript-core/ui/text-base/text-base.ios.ts @@ -153,7 +153,7 @@ export class TextBase extends TextBaseCommon { this.nativeTextViewProtected.setAttributedTitleForState(attrText, UIControlState.Normal); } else { - if (majorVersion >= 13) { + if (majorVersion >= 13 && UIColor.labelColor) { this.nativeTextViewProtected.textColor = UIColor.labelColor; } @@ -205,7 +205,7 @@ export class TextBase extends TextBaseCommon { if (dict.size > 0 || isTextView) { if (style.color) { dict.set(NSForegroundColorAttributeName, style.color.ios); - } else if (majorVersion >= 13) { + } else if (majorVersion >= 13 && UIColor.labelColor) { dict.set(NSForegroundColorAttributeName, UIColor.labelColor); } } diff --git a/nativescript-core/ui/text-view/text-view.ios.ts b/nativescript-core/ui/text-view/text-view.ios.ts index a1411cffd..a45d643d9 100644 --- a/nativescript-core/ui/text-view/text-view.ios.ts +++ b/nativescript-core/ui/text-view/text-view.ios.ts @@ -116,8 +116,8 @@ export class TextView extends EditableTextBase implements TextViewDefinition { private _isShowingHint: boolean; public _isEditing: boolean; - private _hintColor = majorVersion <= 12 ? UIColor.blackColor.colorWithAlphaComponent(0.22) : UIColor.placeholderTextColor; - private _textColor = majorVersion <= 12 ? null : UIColor.labelColor; + private _hintColor = (majorVersion <= 12 || !UIColor.placeholderTextColor) ? UIColor.blackColor.colorWithAlphaComponent(0.22) : UIColor.placeholderTextColor; + private _textColor = (majorVersion <= 12 || !UIColor.labelColor) ? null : UIColor.labelColor; createNativeView() { const textView = NoScrollAnimationUITextView.new(); diff --git a/tests/app/ui/page/page-tests-common.ts b/tests/app/ui/page/page-tests-common.ts index 2a71baeed..c488ddb9d 100644 --- a/tests/app/ui/page/page-tests-common.ts +++ b/tests/app/ui/page/page-tests-common.ts @@ -385,7 +385,7 @@ export function test_page_backgroundColor() { helper.navigate(factory); if (isIOS) { - const backgroundColor = ios.MajorVersion <= 12 ? UIColor.whiteColor : UIColor.systemBackgroundColor; + const backgroundColor = (ios.MajorVersion <= 12 || !UIColor.systemBackgroundColor) ? UIColor.whiteColor : UIColor.systemBackgroundColor; TKUnit.assertEqual(page.nativeView.backgroundColor, backgroundColor, "page backgroundColor is wrong"); } else { const whiteColor = new Color("white");