删除UINavigationBar边框会导致状态栏变黑

本文关键字:状态栏 UINavigationBar 边框 删除 | 更新日期: 2023-09-27 17:57:26

我试图删除UINavigationBar下面的边界,如下所述:

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

在应用程序代理中,我还设置了背景颜色:

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

现在UINavigationBar没有所需的边界,导航栏的颜色也发生了变化。但现在状态栏完全是黑色的,没有显示任何内容。我试图在Info.plist中设置状态栏的样式,但这也没有帮助。

我做错了什么?我是否必须以某种方式设置状态栏的背景?

现在我尝试在一个单独的项目中这样做,并设置导航栏的背景色。这里的状态栏不是黑色的,但状态栏的颜色已经消失了。只有导航栏有颜色,但状态栏保持为白色。通常情况下,通过设置条形图色调,状态栏和导航栏应获得相同的颜色。例如

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

设置条形图色调没有效果,所以我设置了导航栏的背景色。

如何删除导航栏的边框并将状态栏和导航栏设置为相同的颜色?

删除UINavigationBar边框会导致状态栏变黑

我记不清这个是从哪里得到的,但我想它在obj-c中,这就是我使用的。

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();
        }
    }

因为其他方法没有像预期的那样起作用,我现在创建了一个图像,并将其设置为导航栏的背景,如下所示:

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

这里是ImageHelper类:

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;
    }
}