Set navigation bar image in iOS 7

2019-01-22 21:06发布

I want to convert my current project from iOS 6 to iOS 7. In iOS 6 my project is working fine, but in iOS 7 navigation bar image is not showing properly.

I used this code snippet for iOS 6,

UIImage *imgNav = [UIImage imageNamed:@"navigation.png"];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationBar setBackgroundImage:imgNav forBarMetrics:
     UIBarMetricsDefault];

How can I set the navigation bar image in iOS 7?

9条回答
萌系小妹纸
2楼-- · 2019-01-22 21:42

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation.png"] forBarMetrics:UIBarMetricsDefault];

Its working if you follow the rules mentioned in ios7 guide: • If you want a solid color with no gradient, create a 1 x 1 point image. • If you want a vertical gradient, create an image that has a width of 1 point and a height that matches the height of the UI element’s background. • If you want to provide a repeating textured appearance, you need to create an image with dimensions that match the dimensions of the repeating portion of the texture. • If you want to provide a nonrepeating textured appearance, you need to create a static image with dimensions that match the dimensions of the UI element’s background area.

For more Info Please follow the link:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/ResizableImages.html#//apple_ref/doc/uid/TP40006556-CH30-SW1

查看更多
够拽才男人
3楼-- · 2019-01-22 21:50

Just do this..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // This will set the backGround image for all the Navigation Bars

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar"] forBarMetrics:UIBarMetricsDefault];



    return YES;
}
查看更多
欢心
4楼-- · 2019-01-22 21:57
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] )
{

    UIImage *image = [UIImage imageNamed:@"navigation.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
查看更多
登录 后发表回答