From c31346f38cba2888838c5cfb6bb3f3c49d87dfe2 Mon Sep 17 00:00:00 2001 From: valera1401 <33029076+valera1401@users.noreply.github.com> Date: Tue, 28 Sep 2021 20:29:24 +0300 Subject: [PATCH] fix(action-bar): use appearance api on ios15+ (#9583) Co-authored-by: Igor Randjelovic --- .../ui/action-bar/action-bar.ios.ts | 76 +++++++++++++++---- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/nativescript-core/ui/action-bar/action-bar.ios.ts b/nativescript-core/ui/action-bar/action-bar.ios.ts index 97a8aa143..5c953b350 100644 --- a/nativescript-core/ui/action-bar/action-bar.ios.ts +++ b/nativescript-core/ui/action-bar/action-bar.ios.ts @@ -85,7 +85,6 @@ export class NavigationButton extends ActionItem { } export class ActionBar extends ActionBarBase { - get ios(): UIView { const page = this.page; if (!page || !page.parent) { @@ -292,13 +291,21 @@ export class ActionBar extends ActionBarBase { this.setColor(navBar, color); const bgColor = this.backgroundColor; - navBar.barTintColor = bgColor ? bgColor.ios : null; + this.setBackgroundColor(navBar, bgColor); } private setColor(navBar: UINavigationBar, color?: Color) { + if (!navBar) { + return; + } if (color) { - navBar.titleTextAttributes = { [NSForegroundColorAttributeName]: color.ios }; - navBar.largeTitleTextAttributes = { [NSForegroundColorAttributeName]: color.ios }; + const titleTextColor = NSDictionary.dictionaryWithObjectForKey(color.ios, NSForegroundColorAttributeName); + if (majorVersion >= 15) { + const appearance = navBar.standardAppearance ?? UINavigationBarAppearance.new(); + appearance.titleTextAttributes = titleTextColor; + } + navBar.titleTextAttributes = titleTextColor; + navBar.largeTitleTextAttributes = titleTextColor; navBar.tintColor = color.ios; } else { navBar.titleTextAttributes = null; @@ -307,6 +314,25 @@ export class ActionBar extends ActionBarBase { } } + private setBackgroundColor(navBar: UINavigationBar, color?: UIColor | Color) { + if (!navBar) { + return; + } + + const color_ = color instanceof Color ? color.ios : color; + if (majorVersion >= 15) { + const appearance = navBar.standardAppearance ?? UINavigationBarAppearance.new(); + // appearance.configureWithOpaqueBackground(); + appearance.backgroundColor = color_; + navBar.standardAppearance = appearance; + navBar.compactAppearance = appearance; + navBar.scrollEdgeAppearance = appearance; + } else { + // legacy styling + navBar.barTintColor = color_; + } + } + public _onTitlePropertyChanged() { const page = this.page; if (!page) { @@ -323,13 +349,34 @@ export class ActionBar extends ActionBarBase { private updateFlatness(navBar: UINavigationBar) { if (this.flat) { - navBar.setBackgroundImageForBarMetrics(UIImage.new(), UIBarMetrics.Default); - navBar.shadowImage = UIImage.new(); - navBar.translucent = false; + if (majorVersion >= 15) { + const appearance = navBar.standardAppearance ?? UINavigationBarAppearance.new(); + appearance.shadowColor = UIColor.clearColor; + + navBar.standardAppearance = appearance; + navBar.compactAppearance = appearance; + navBar.scrollEdgeAppearance = appearance; + } else { + + navBar.setBackgroundImageForBarMetrics(UIImage.new(), UIBarMetrics.Default); + navBar.shadowImage = UIImage.new(); + navBar.translucent = false; + } } else { - navBar.setBackgroundImageForBarMetrics(null, null); - navBar.shadowImage = null; - navBar.translucent = true; + if (majorVersion >= 15) { + if(navBar.standardAppearance){ // Not flat and never been set do nothing. + const appearance = navBar.standardAppearance; + appearance.shadowColor = UINavigationBarAppearance.new().shadowColor; + + navBar.standardAppearance = appearance; + navBar.compactAppearance = appearance; + navBar.scrollEdgeAppearance = appearance; + } + } else { + navBar.setBackgroundImageForBarMetrics(null, null); + navBar.shadowImage = null; + navBar.translucent = true; + } } } @@ -404,12 +451,9 @@ export class ActionBar extends ActionBarBase { // CssAnimationProperty use default value form their constructor. return null; } - [backgroundColorProperty.setNative](value: UIColor | Color) { - let navBar = this.navBar; - if (navBar) { - let color = value instanceof Color ? value.ios : value; - navBar.barTintColor = color; - } + [backgroundColorProperty.setNative](color: UIColor | Color) { + const navBar = this.navBar; + this.setBackgroundColor(navBar, color); } [backgroundInternalProperty.getDefault](): UIColor {