How to set opaque 1px high shadow image for UINavigationBar?

In my delegate doing the didFinishLaunchingWithOptions function, I am trying to customize the look of my navigation bar.

[UINavigationBar appearance].translucent = NO; [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1.0f, 1.0f) ] forBarMetrics:UIBarMetricsDefault ]; [UINavigationBar appearance].shadowImage = [UIImage imageWithColor:[UIColor redColor] size:CGSizeMake(0.5f, 0.5f) ]; 

I expect a red shadow 1px thick is high. Instead, they give me a 2px high translucent red shadow. How can it look the way I want? I made similar appearance settings for UITabBar. This, on the other hand, behaves well.

enter image description here

The category function that creates dynamic images is defined as follows:

 + (UIImage*)imageWithColor:(UIColor *)color size:(CGSize)size { CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 
+8
ios cocoa-touch uinavigationbar uiappearance
source share
2 answers

You need to create a graphics context that the retina supports:

 let pixelScale = UIGraphicsBeginImageContextWithOptions(fillRect.size, false, UIScreen.main.scale) 

After that, your shadow image will become 1 physical pixel tall.

Here is the full code:

 extension UIImage { static func imageWithColor(color: UIColor) -> UIImage { let pixelScale = UIScreen.main.scale let pixelSize = 1 / pixelScale let fillSize = CGSize(width: pixelSize, height: pixelSize) let fillRect = CGRect(origin: CGPoint.zero, size: fillSize) UIGraphicsBeginImageContextWithOptions(fillRect.size, false, pixelScale) let graphicsContext = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(graphicsContext, color.CGColor) CGContextFillRect(graphicsContext, fillRect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } 

In github:

https://github.com/salutis/swift-image-with-color

+15
source share

Uh ... is that what you want?

screenshot

In my opinion, the controller, I just added a UIView with a height of 1 pixel and an offset of 44 pixels:

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; [self setupNavBar]; } -(void)setupNavBar { self.navigationItem.title = @"Contacts"; CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 44, applicationFrame.size.width, 1.0)]; bar.backgroundColor = [UIColor redColor]; [self.navigationController.navigationBar addSubview:bar]; } 

You can change the color to whatever you want.

-one
source share

All Articles