void UpdateStyle() { var navigationBarAppearance = NavigationBar.StandardAppearance; var titleTextAttributes = new UIStringAttributes { Font = UIFont.FromName((string)Xamarin.Forms.Application.Current.Resources["font_semibold"], 16) }; var title = new UIStringAttributes { ForegroundColor = Xamarin.Forms.Application.Current.RequestedTheme == OSAppTheme.Light ? ((Color)Xamarin.Forms.Application.Current.Resources["black"]).ToUIColor() : ((Color)Xamarin.Forms.Application.Current.Resources["white"]).ToUIColor(), Font = UIFont.FromName((string)Xamarin.Forms.Application.Current.Resources["font_bold"], 18) }; var titleText = new UITextAttributes { TextColor = title.ForegroundColor, Font = title.Font }; navigationBarAppearance.TitleTextAttributes = title; if (titleTextAttributes.Dictionary.Keys.Any()) { var dic = new NSDictionary <NSString, NSObject>( new NSString[] { titleTextAttributes.Dictionary.Keys[0] as NSString }, titleTextAttributes.Dictionary.Values ); var button = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain); button.Normal.TitleTextAttributes = dic; button.Highlighted.TitleTextAttributes = dic; navigationBarAppearance.ButtonAppearance = button; } NavigationBar.StandardAppearance = navigationBarAppearance; NavigationBar.CompactAppearance = navigationBarAppearance; NavigationBar.ScrollEdgeAppearance = navigationBarAppearance; UINavigationBar.Appearance.SetTitleTextAttributes(titleText); }
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; }