Getting rid of 1px shadow on a UINavigationBar with a custom background image

I am creating a custom UINavigationBarwith the image of the top bar with a black gradient. How can I kill this 1px shadow border below?

I tried the code that I found here, but it didn’t work on my device, just a simulator. Here is the code I tried.

override func viewDidLoad() {
    super.viewDidLoad()
    UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: false)
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"TripNavigationBar2.png"), forBarPosition: .Any, barMetrics: .Default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
}

The insert viewDidAppear:didn't work either.

Can someone explain why and how I can fix this? only on sim?

+4
source share
6 answers

Your code is ok.

, . @2x, . iPhone 5s. , :

Resized image

TripNavigationBar2@2x.png, .

UIImage(named:"TripNavigationBar2") ( .png), .

+2

, UIImage -shadowImage .
, objC:

[[UINavigationBar appearance] setShadowImage:[UIImage  new]];

, , shadowImage. , , ? didFinishLauching.
1px . , .

+ (UIImage *) imageWithColor:(UIColor*) color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return colorImage;
}
+1

:

self.navigationController?.navigationBar.clipsToBounds
+1

, .

- YES.

[self.navigationController.navigationBar setTranslucent:NO];

, , .

+1

iPad Air UIImage, 1px . , UIImage, UIView - , . - UIImage .

, , , , . , . - . , , , .

, , , iPhone 6 , .

+1

All Articles