简体   繁体   中英

Removing of UINavigationBar border leads to black status bar

I tried to remove the border below the UINavigationBar as described here :

NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage ();

In the app delegate I also set a background color:

UINavigationBar.Appearance.BackgroundColor = UIColor.FromRGB (247, 247, 247);

Now the UINavigationBar has no border as desired and also the color of the navigation bar has changed. But now the status bar is completely black not showing anything. I tried to set the style of the status bar in the Info.plist but that didn't helped either.

What I'm doing wrong? Do I have to set the background for the status bar somehow?

Now I tried to do that in a separate project and set the background color of the navigation bar. Here the status bar isn't black but the color for the status bar is gone. Only the navigation bar had a color, but the status bar stayed white. Normally through setting the bar tint color the status bar and the navigation bar should get the same color. Eg

[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];

Setting the bar tint color didn't had an effect so I set the background color of the navigation bar.

How do I remove the border of the navigation bar and set the status and navigation bar to the same color?

I can't remember where I got this but I think it was in obj-c, it's what I use.

public static class UINavigationBarExtensions
    {
        public static void RemoveShadowImage(this UINavigationBar navigationBar)
        {
            foreach (var image in 
                from subView in navigationBar.Subviews
                select subView as UIImageView
                into imageView
                where imageView != null && imageView.ToString()
                    .Contains("BarBackground")
                from image in imageView.Subviews.OfType<UIImageView>()
                    .Where(image => Math.Abs(image.Bounds.Height - 0.5) < float.Epsilon)
                select image)
                image.RemoveFromSuperview();
        }
    }

Because the other approaches didn't worked as expected I now create an image and set this as background for the navigation bar like so:

UIImage backgroundImage = ImageHelper.ImageWithColor (UINavigationBar.Appearance.BarTintColor, new CGRect (0, 0, 1, 1));
NavigationController.NavigationBar.SetBackgroundImage (backgroundImage, UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage ();

and here the ImageHelper class:

using System;
using System.Drawing;

using CoreGraphics;
using Foundation;
using UIKit;

public class ImageHelper
{
    public ImageHelper ()
    {
    }

    public static UIImage ImageWithColor(UIColor color, CGRect rect){
        CGRect rectangleForImage = new CGRect (0, 0, rect.Width, rect.Height);
        UIGraphics.BeginImageContext (rectangleForImage.Size);
        CGContext context = UIGraphics.GetCurrentContext ();

        context.SetFillColor (color.CGColor);
        context.FillRect (rectangleForImage);

        UIImage image = UIGraphics.GetImageFromCurrentImageContext ();
        UIGraphics.EndImageContext ();

        return image;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM