在 iOS 7 中设置导航栏图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17361500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set navigation bar image in iOS 7
提问by Mayuri R Talaviya
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.
我想将我当前的项目从 iOS 6 转换为 iOS 7。在 iOS 6 中我的项目工作正常,但在 iOS 7 导航栏图像显示不正确。
I used this code snippet for iOS 6,
我将此代码片段用于 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?
如何在 iOS 7 中设置导航栏图像?
回答by iCoder
Try Adding the below code in AppDelegate
尝试在 AppDelegate 中添加以下代码
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation.png"]
forBarMetrics:UIBarMetricsDefault];
This is the Swift Version:
这是 Swift 版本:
UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "navigation.png"), forBarMetrics: UIBarMetrics.Default)
Swift 3 version :
斯威夫特 3 版本:
UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "logo-dark.png"), for: UIBarMetrics.default)
回答by Preet
For iOS 7:
对于 iOS 7:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
回答by Priyank Jotangiya
Use This Simple Syntax for Change Navigation Background
Easy Way.
使用这个简单的语法来改变Navigation Background
简单的方法。
self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YourImage.png"]];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
回答by Pradeep
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] )
{
UIImage *image = [UIImage imageNamed:@"navigation.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
回答by Will
The Storyboard Way:
故事板方式:
- Drag an image view onto the bottom bar of the storyboard scene.
- Control-drag from the navigation item on the left in the scene list to the newly created image view.
- Click on the image view, and set the image in the attribute.
- 将图像视图拖到故事板场景的底部栏上。
- 按住 Control 键从场景列表左侧的导航项拖动到新创建的图像视图。
- 单击图像视图,并在属性中设置图像。
回答by Gokul
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;
}
回答by Sandip Patel - SM
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar 外观] 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.
如果您遵循 ios7 指南中提到的规则,它就可以工作:如果您想要没有渐变的纯色,请创建 1 x 1 点图像。? 如果您想要垂直渐变,请创建一个宽度为 1 磅且高度与 UI 元素背景高度匹配的图像。? 如果要提供重复纹理外观,则需要创建尺寸与纹理重复部分尺寸匹配的图像。? 如果要提供不重复的纹理外观,则需要创建尺寸与 UI 元素背景区域尺寸相匹配的静态图像。
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
更多信息请点击链接:https:
//developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/ResizableImages.html#//apple_ref/doc/uid/TP40006556-CH30-SW1
回答by Paresh Hirpara
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setTitleTextAttributes: @{
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:@"AppleGothic" size:20.0f]
}];
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigatio_for_ios6"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
// Uncomment to change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigon_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];
// Uncomment to change the font style of the title
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
}
回答by Shahzaib Maqbool
Try this code in appDelegate class it will help you.
在 appDelegate 类中试试这个代码,它会帮助你。
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"navbarimg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];