删除 UINavigationBar 边框导致黑色状态栏
Removing of UINavigationBar border leads to black status bar
我试图按照 here:
的描述移除 UINavigationBar
下方的边框
NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage ();
在应用委托中我还设置了背景颜色:
UINavigationBar.Appearance.BackgroundColor = UIColor.FromRGB (247, 247, 247);
现在 UINavigationBar
没有所需的边框,导航栏的颜色也已更改。但是现在状态栏是完全黑色的,没有任何显示。我尝试在 Info.plist 中设置状态栏的样式,但这也没有帮助。
我做错了什么?我必须以某种方式设置状态栏的背景吗?
现在我尝试在一个单独的项目中这样做并设置导航栏的背景颜色。此处状态栏不是黑色,但状态栏的颜色消失了。只有导航栏有颜色,但状态栏保持白色。 Normally 通过设置栏色调颜色,状态栏和导航栏应该得到相同的颜色。例如
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
设置栏色调没有效果,所以我设置了导航栏的背景颜色。
如何去掉导航栏的边框,并将状态栏和导航栏设置成同一种颜色?
因为其他方法没有按预期工作我现在创建一个图像并将其设置为导航栏的背景,如下所示:
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
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;
}
}
我不记得我是从哪里得到这个的,但我认为它是在 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();
}
}
我试图按照 here:
的描述移除UINavigationBar
下方的边框
NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage ();
在应用委托中我还设置了背景颜色:
UINavigationBar.Appearance.BackgroundColor = UIColor.FromRGB (247, 247, 247);
现在 UINavigationBar
没有所需的边框,导航栏的颜色也已更改。但是现在状态栏是完全黑色的,没有任何显示。我尝试在 Info.plist 中设置状态栏的样式,但这也没有帮助。
我做错了什么?我必须以某种方式设置状态栏的背景吗?
现在我尝试在一个单独的项目中这样做并设置导航栏的背景颜色。此处状态栏不是黑色,但状态栏的颜色消失了。只有导航栏有颜色,但状态栏保持白色。 Normally 通过设置栏色调颜色,状态栏和导航栏应该得到相同的颜色。例如
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
设置栏色调没有效果,所以我设置了导航栏的背景颜色。
如何去掉导航栏的边框,并将状态栏和导航栏设置成同一种颜色?
因为其他方法没有按预期工作我现在创建一个图像并将其设置为导航栏的背景,如下所示:
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
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;
}
}
我不记得我是从哪里得到这个的,但我认为它是在 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();
}
}