问题
I am trying to change Navigation bar background to Gradient Red. I have added subview to navigation bar which sets background to Color I want. but then Navigation bar back button Text is invisible. Back button is visible but Text is not Visible.
UIView view = new UIView();
var gradient = new CAGradientLayer();
gradient.Frame = NavigationBar.Bounds;
gradient.NeedsDisplayOnBoundsChange = true;
gradient.MasksToBounds = true;
gradient.Colors = new CGColor[] { UIColor.FromRGB(248, 0, 0).CGColor, UIColor.FromRGB(143, 0, 0).CGColor };
view.Layer.InsertSublayer(gradient, 0);
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes() { TextColor = UIColor.Clear });
var bounds = NavigationBar.Bounds;
this.NavigationBar.AddSubview(view);
回答1:
I created image out of gradient color and used that image as Background to NavigationBar. For iOS see below tutorial.
UINavigationBar tintColor with gradient
UIGraphics.BeginImageContext(gradient.Bounds.Size);
gradient.RenderInContext(UIGraphics.GetCurrentContext());
UIImage backImage = Graphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
NavigationBar.BarStyle = UIBarStyle.Default;
UINavigationBar.Appearance.SetBackgroundImage(backImage, UIBarMetrics.Default);
来源:https://stackoverflow.com/questions/30701578/uiview-hides-navigation-bar-back-button-text-how-to-make-it-visible