【问题标题】:Removing of UINavigationBar border leads to black status bar移除 UINavigationBar 边框会导致黑色状态栏
【发布时间】:2015-06-22 10:37:54
【问题描述】:

我尝试删除UINavigationBar 下方的边框,如here 所述:

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

在应用委托中,我还设置了背景颜色:

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

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

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

现在我尝试在一个单独的项目中执行此操作并设置导航栏的背景颜色。此处状态栏不是黑色,但状态栏的颜色消失了。只有导航栏有颜色,但状态栏保持白色。 Normally 通过设置 bar tint color 状态栏和导航栏应该得到相同的颜色。例如

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

设置 bar tint color 没有效果,所以我设置了导航栏的背景颜色。

如何去掉导航栏的边框,并将状态和导航栏设置为同色?

【问题讨论】:

    标签: c# ios xamarin xamarin.ios uinavigationbar


    【解决方案1】:

    我不记得我从哪里得到的,但我认为它是在 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();
            }
        }
    

    【讨论】:

    • 不错的一个! NavigationController.NavigationBar.RemoveShadowImage(); 成功了!那么只有阴影图像被删除了吗?正如我所看到的,该函数获取UINavigationBar 的所有子视图,将它们映射到UIImageView,在 BarBackground 之后搜索,进一步进入子视图,查找某个高度并删除它们。我说的对吗?
    • 在我的情况下,我必须添加 NavigationController.NavigationBar.Translucent = false; 才能在导航栏上获得正确的颜色。
    • 我还没有找到我得到这个的来源,如果我找到了,我会更新。
    • 现在我在 iPad 上进行了测试,在这里它似乎不起作用。这条线还在这里。 iPad 上的方法有什么不同吗?
    • 有趣,我没有在 iPad 上尝试过。如果设置断点,navigationBar.Subviews 是否包含该 imageView?
    【解决方案2】:

    由于其他方法没有按预期工作,我现在创建一个图像并将其设置为导航栏的背景,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多