protected override void Render() { // Icon & Content if (Element.Icon != null) { switch (Element.Icon) { case BitmapIcon bitmap: Native.Image = UIImageHelper.FromUri(bitmap.UriSource); Native.CustomView = null; Native.Title = null; break; case FontIcon font: // not supported case PathIcon path: // not supported case SymbolIcon symbol: // not supported default: this.Log().WarnIfEnabled(() => $"{GetType().Name ?? "FontIcon, PathIcon and SymbolIcon"} are not supported. Use BitmapIcon instead with UriSource."); Native.Image = null; Native.CustomView = null; // iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set. // We default to an empty string to ensure it is added. Native.Title = string.Empty; break; } } else { switch (Element.Content) { case string text: Native.Image = null; Native.CustomView = null; Native.Title = text; break; case FrameworkElement fe: var currentParent = Element.GetParent(); _appBarButtonWrapper.Child = Element; //Restore the original parent if any, as we // want the DataContext to flow properly from the // CommandBar. Element.SetParent(currentParent); Native.Image = null; Native.CustomView = fe.Visibility == Visibility.Visible ? _appBarButtonWrapper : null; // iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set. // We default to an empty string to ensure it is added, in order to support late-bound Content. Native.Title = string.Empty; break; default: Native.Image = null; Native.CustomView = null; // iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set. // We default to an empty string to ensure it is added. Native.Title = string.Empty; break; } } // Label Native.AccessibilityHint = Element.Label; Native.AccessibilityLabel = Element.Label; // Foreground if (Element.Foreground is SolidColorBrush foreground) { var color = (UIColor)foreground.Color as UIColor; var alpha = (nfloat)(foreground.Opacity * Element.Opacity); Native.TintColor = color.ColorWithAlpha(alpha); } else { Native.TintColor = default(UIColor); // TODO .Clear; } // IsEnabled Native.Enabled = Element.IsEnabled; // Background var backgroundColor = (Element.Background as SolidColorBrush)?.ColorWithOpacity; if (backgroundColor != null) { if (HasContent) { // Setup the background color when a custom content is set. _appBarButtonWrapper.BackgroundColor = backgroundColor; } else { var isTransparent = backgroundColor.Value.A == 0; var backgroundImage = isTransparent ? new UIImage() // Clears the background : ((UIColor)backgroundColor).ToUIImage(); // Applies the solid color; // We're using SetBackgroundImage instead of SetBackgroundColor // because it extends all the way up under the status bar. Native.SetBackgroundImage(backgroundImage, UIControlState.Normal, UIBarMetrics.Default); } } }
protected override void Render() { ApplyVisibility(); // Foreground if (Brush.TryGetColorWithOpacity(Element.Foreground, out var foregroundColor)) { Native.TitleTextAttributes = new UIStringAttributes { ForegroundColor = foregroundColor, }; } else { Native.TitleTextAttributes = null; } // Background var backgroundColor = Brush.GetColorWithOpacity(Element.Background); switch (backgroundColor) { case Color opaqueColor when opaqueColor.A == byte.MaxValue: // 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 Color semiTransparentColor when semiTransparentColor.A > 0: 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 Color transparent when transparent.A == 0: 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 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; } // 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) : null; Native.BackIndicatorImage = backButtonIcon; Native.BackIndicatorTransitionMaskImage = backButtonIcon; if (Element.Presenter != null) { Element.Presenter.Height = Native.Hidden ? 0 : Native.Frame.Size.Height; } }
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; }