0

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. E.g.

[[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?

Community
  • 1
  • 1
testing
  • 19,681
  • 50
  • 236
  • 417

2 Answers2

1

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();
        }
    }
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
  • Nice one! `NavigationController.NavigationBar.RemoveShadowImage();` did the trick! So only the shadow image is removed? As I can see that function takes all subviews of `UINavigationBar`, maps them onto `UIImageView`, searches after *BarBackground*, goes further into the subviews, looks for a certain height and removes them. Am I right? – testing Jun 23 '15 at 07:58
  • In my case I had to add `NavigationController.NavigationBar.Translucent = false;` to get the correct color on the navigation bar. – testing Jun 23 '15 at 08:07
  • I haven't found the source of where I got this, if I do, I'll update. – Derek Beattie Jun 24 '15 at 16:54
  • Now I tested this on iPad and here it doesn't seem to work. The line is still here. Is the approach on the iPad differently? – testing Jul 06 '15 at 13:58
  • Interesting, I didn't try it on the iPad. If you set a breakpoint does navigationBar.Subviews contain that imageView? – Derek Beattie Jul 06 '15 at 14:24
  • There is a `_UINavigationBarBackground` which contains a `_UIBackDropView` and an `UIImageView` as subview. The latter one has the height one which seems to be the line. On iPad this view isn't removed through the LINQ call. I think the reason is the height check. On iPhone it is only 0.5 whereas on iPad it is 1 and comparing with `float.Epsilon` fails here. Using e.g. `.Where(image => Math.Abs(image.Bounds.Height) < 2)` does work here, but I don't know the consequences of using this ... – testing Jul 07 '15 at 07:58
  • For that it should be fine. – Derek Beattie Jul 07 '15 at 14:16
0

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;
    }
}
testing
  • 19,681
  • 50
  • 236
  • 417