public static void ApplyTheme(this UINavigationBar navigationBar, NavigationTheme theme) { navigationBar.PrefersLargeTitles = theme.PrefersLargeTitles; var regularNavigationAppearance = new UINavigationBarAppearance(); regularNavigationAppearance.BackgroundColor = theme.BackgroundColor; regularNavigationAppearance.TitleTextAttributes = new UIStringAttributes() { ForegroundColor = theme.TextColor }; navigationBar.StandardAppearance = regularNavigationAppearance; if (theme.PrefersLargeTitles) { var largeNavigationAppearance = new UINavigationBarAppearance(); largeNavigationAppearance.BackgroundImageContentMode = UIViewContentMode.ScaleAspectFill; largeNavigationAppearance.BackgroundImage = theme.BackgroundImage; largeNavigationAppearance.ShadowColor = UIColor.Clear; largeNavigationAppearance.LargeTitleTextAttributes = new UIStringAttributes() { ForegroundColor = theme.TextColor }; navigationBar.ScrollEdgeAppearance = largeNavigationAppearance; } navigationBar.TintColor = theme.TextColor; }
void UpdateiOS15NavigationBarAppearance(UINavigationController controller, ShellAppearance appearance) { var navBar = controller.NavigationBar; var navigationBarAppearance = new UINavigationBarAppearance(); navigationBarAppearance.ConfigureWithOpaqueBackground(); navBar.Translucent = false; // Set ForegroundColor var foreground = appearance.ForegroundColor; if (foreground != null) { navBar.TintColor = foreground.ToPlatform(); } // Set BackgroundColor var background = appearance.BackgroundColor; if (background != null) { navigationBarAppearance.BackgroundColor = background.ToPlatform(); } // Set TitleColor var titleColor = appearance.TitleColor; if (titleColor != null) { navigationBarAppearance.TitleTextAttributes = new UIStringAttributes() { ForegroundColor = titleColor.ToPlatform() } } ; navBar.StandardAppearance = navBar.ScrollEdgeAppearance = navigationBarAppearance; } void UpdateNavigationBarAppearance(UINavigationController controller, ShellAppearance appearance) { var background = appearance.BackgroundColor; var foreground = appearance.ForegroundColor; var titleColor = appearance.TitleColor; var navBar = controller.NavigationBar; if (background != null) { navBar.BarTintColor = background.ToPlatform(); } if (foreground != null) { navBar.TintColor = foreground.ToPlatform(); } if (titleColor != null) { navBar.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor.ToPlatform() }; } } }
protected override void Render() { ApplyVisibility(); var appearance = new UINavigationBarAppearance(); // Background var backgroundColor = Brush.GetColorWithOpacity(Element.Background); switch (backgroundColor) { case { } opaqueColor when opaqueColor.A == byte.MaxValue: if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { appearance.ConfigureWithOpaqueBackground(); appearance.BackgroundColor = opaqueColor; } else { // Prefer BarTintColor because it supports smooth transitions Native.BarTintColor = opaqueColor; Native.Translucent = false; //Make fully opaque for consistency with SetBackgroundImage Native.SetBackgroundImage(null, UIBarMetrics.Default); Native.ShadowImage = null; } break; case { } semiTransparentColor when semiTransparentColor.A > 0: if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { appearance.ConfigureWithDefaultBackground(); appearance.BackgroundColor = semiTransparentColor; } else { Native.BarTintColor = null; // Use SetBackgroundImage as hack to support semi-transparent background Native.SetBackgroundImage(((UIColor)semiTransparentColor).ToUIImage(), UIBarMetrics.Default); Native.Translucent = true; Native.ShadowImage = null; } break; case { } transparent when transparent.A == 0: if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { appearance.ConfigureWithTransparentBackground(); appearance.BackgroundColor = transparent; } else { Native.BarTintColor = null; Native.SetBackgroundImage(new UIImage(), UIBarMetrics.Default); // We make sure a transparent bar doesn't cast a shadow. Native.ShadowImage = new UIImage(); // Removes the default 1px line Native.Translucent = true; } break; default: //Background is null if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { appearance.ConfigureWithDefaultBackground(); appearance.BackgroundColor = null; } else { Native.BarTintColor = null; Native.SetBackgroundImage(null, UIBarMetrics.Default); // Restores the default blurry background Native.ShadowImage = null; // Restores the default 1px line Native.Translucent = true; } break; } // Foreground if (Brush.TryGetColorWithOpacity(Element.Foreground, out var foregroundColor)) { if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { appearance.TitleTextAttributes = new UIStringAttributes { ForegroundColor = foregroundColor, }; appearance.LargeTitleTextAttributes = new UIStringAttributes { ForegroundColor = foregroundColor, }; } else { Native.TitleTextAttributes = new UIStringAttributes { ForegroundColor = foregroundColor, }; } } else { if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { appearance.TitleTextAttributes = new UIStringAttributes(); appearance.LargeTitleTextAttributes = new UIStringAttributes(); } else { Native.TitleTextAttributes = null; } } // CommandBarExtensions.BackButtonForeground var backButtonForeground = Brush.GetColorWithOpacity(Element.GetValue(BackButtonForegroundProperty) as Brush); Native.TintColor = backButtonForeground; // CommandBarExtensions.BackButtonIcon var backButtonIcon = Element.GetValue(BackButtonIconProperty) is BitmapIcon bitmapIcon ? UIImageHelper.FromUri(bitmapIcon.UriSource)?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) : null; if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { var backButtonAppearance = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain); if (backButtonForeground is { } foreground) { var titleTextAttributes = new UIStringAttributes { ForegroundColor = foreground }; var attributes = new NSDictionary <NSString, NSObject>( new NSString[] { titleTextAttributes.Dictionary.Keys[0] as NSString }, titleTextAttributes.Dictionary.Values ); backButtonAppearance.Normal.TitleTextAttributes = attributes; backButtonAppearance.Highlighted.TitleTextAttributes = attributes; if (backButtonIcon is { } image) { var tintedImage = image.ApplyTintColor(foreground); appearance.SetBackIndicatorImage(tintedImage, tintedImage); } } else { if (backButtonIcon is { } image) { appearance.SetBackIndicatorImage(image, image); } } appearance.BackButtonAppearance = backButtonAppearance; }