/// <summary> /// Applies a theme to the navigation bar. Note, this does not update the theme for the title. /// </summary> /// <param name="navigationBar">A navigation bar to apply the theme to.</param> /// <param name="theme">The theme to apply to the navigation bar.</param> public static void ApplyTheme(this UINavigationBar navigationBar, NavigationBarTheme theme) { UIColor backgroundColor = theme.BackgroundColor.ToUIColor(); navigationBar.BackgroundColor = backgroundColor; navigationBar.BarTintColor = backgroundColor; navigationBar.Translucent = theme.BackgroundColor.Alpha == 0; navigationBar.TintColor = theme.ItemColor.ToUIColor(); if (theme.ShadowColor.Alpha > 0) { nfloat height = 1.0f / UIScreen.MainScreen.Scale; UIView shadow = new UIView(new CGRect(0, navigationBar.Frame.Height, navigationBar.Frame.Width, height)); shadow.BackgroundColor = theme.ShadowColor.ToUIColor(); navigationBar.ShadowImage = null; navigationBar.AddSubview(shadow); } else { navigationBar.ShadowImage = new UIImage(); } UIStringAttributes attributes = new UIStringAttributes(); attributes.Font = ViewThemeExtensions.GetFont(theme.FontName, ViewFontSizes.LargeFontSize, FontStyle.Bold); attributes.ForegroundColor = theme.FontColor.ToUIColor(); navigationBar.TitleTextAttributes = attributes; }
private void SetupNavigationBar() { // Transparent background UIImage emptyImage = new UIImage(); // NavigationController.NavigationBar.Translucent = true; NavigationController.NavigationBar.Translucent = false; NavigationController.NavigationBar.BarTintColor = ColorHelper.FromType(ColorType.Navigationbar); // NavigationController.NavigationBar.SetBackgroundImage(emptyImage, UIBarMetrics.Default); // NavigationController.NavigationBar.ShadowImage = emptyImage; // NavigationController.NavigationBar.BackgroundColor = UIColor.Red; UINavigationBar navigationBar = NavigationController.NavigationBar; // NSObject objValue = NSObject.FromObject(false); // NavigationController.NavigationBar.SetValueForKey(objValue, new NSString("hidesShadow")); if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0)) { var userInterfaceStyle = TraitCollection.UserInterfaceStyle; if (userInterfaceStyle == UIUserInterfaceStyle.Dark) { logoImageView = new UIImageView(UIImage.FromBundle("logo").ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)); logoImageView.TintColor = ColorHelper.FromType(ColorType.Logo); } else { logoImageView = new UIImageView(UIImage.FromBundle("logo")); } } else { logoImageView = new UIImageView(UIImage.FromBundle("logo")); } // logoImageView.ContentMode = UIViewContentMode.ScaleAspectFit; double imageHeight = navigationBar.Bounds.Height * 0.7; double computedImageWidth = (imageHeight * logoImageView.Image.CGImage.Width) / logoImageView.Image.CGImage.Height; logoImageView.Frame = new CGRect((navigationBar.Bounds.Width / 2) - (computedImageWidth / 2), (navigationBar.Bounds.Height / 2) - (imageHeight / 2), computedImageWidth, imageHeight); navigationBar.AddSubview(logoImageView); NavigationItem.RightBarButtonItem.TintColor = ColorHelper.FromType(ColorType.NavigationbarTint); }
// Custom Methods protected void StyleNavigationBar(UIColor primaryColor, UIColor primaryDarkColor, string title) { this.NavigationController.SetNavigationBarHidden(true, false); UINavigationBar newNavBar = new UINavigationBar(new CGRect(0, 0, this.View.Bounds.Width, 104.0)); newNavBar.BarTintColor = primaryColor; newNavBar.TintColor = UIColor.White; newNavBar.Translucent = false; NavItem = new UINavigationItem(); NavItem.HidesBackButton = true; var titleView = new UIView(new CGRect(0, 0, this.View.Bounds.Width, 88.0f)); var titleLabel = new UILabel(new CGRect(0, 0, this.View.Bounds.Width, 88.0f)); titleLabel.Font = UIFont.SystemFontOfSize(17, UIFontWeight.Semibold); titleLabel.TextColor = UIColor.White; titleLabel.AdjustsFontSizeToFitWidth = true; titleLabel.Text = title; titleView.AddSubview(titleLabel); titleView.AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleBottomMargin; titleLabel.TranslatesAutoresizingMaskIntoConstraints = false; NSLayoutConstraint trailing = NSLayoutConstraint.Create(titleLabel, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, titleView, NSLayoutAttribute.Trailing, 1.0f, 0.0f); NSLayoutConstraint leading = NSLayoutConstraint.Create(titleLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, titleView, NSLayoutAttribute.Leading, 1.0f, 0.0f); NSLayoutConstraint bottom = NSLayoutConstraint.Create(titleLabel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, titleView, NSLayoutAttribute.Bottom, 1.0f, 0.0f); NSLayoutConstraint top = NSLayoutConstraint.Create(titleLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, titleView, NSLayoutAttribute.Top, 1.0f, 0.0f); titleView.AddConstraint(trailing); titleView.AddConstraint(leading); titleView.AddConstraint(bottom); titleView.AddConstraint(top); NavItem.TitleView = titleView; var statusBarView = new UIView(new CGRect(0, 0, this.View.Bounds.Width, 20)); statusBarView.BackgroundColor = primaryDarkColor; newNavBar.SetItems(new[] { NavItem }, false); newNavBar.AddSubview(statusBarView); this.View.AddSubview(newNavBar); }