Changing The UINavigationBar Background Image
Answer :
Starting in iOS 5 you should use the -setBackgroundImage:forBarMetrics:
method:
[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"] forBarMetrics:UIBarMetricsDefault];
And in Swift 4:
navigationBar.setBackgroundImage(UIImage(named: "UINavigationBarBackground.png"), for: .default)
Considering all iOS versions, this seems to be accomplishing both Custom background image and Custom size of UINavigationBar:
@interface CustomNavigationBar : UINavigationBar @end @implementation CustomNavigationBar -(void) drawRect:(CGRect)rect { UIImage *image = [UIImage imageNamed: @"navigationBar"]; [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; //for iOS5 [self setBackgroundImage:[UIImage imageNamed: @"navigationBar"] forBarMetrics:UIBarMetricsDefault]; } //for custom size of the UINavigationBar - (CGSize)sizeThatFits:(CGSize)size { CGSize newSize = CGSizeMake(320,31); return newSize; } @end
I use such codes in a common place like a Utilities file.
Hope this helps.
Comments
Post a Comment