phi*_*son 53 cocoa-touch objective-c uinavigationbar ios
可能重复:
如何在iphone导航栏上添加背景图片?
iOS - 纽约时报如何做这个自定义顶部导航栏样式?
就此而言,最底层的一个?

Chr*_*orr 143
@ ludwigschubert的解决方案也不过不是在iOS 5的工作同样没有覆盖-drawRect:的,因为它甚至没有叫.
从iOS 5开始,导航栏由a UINavigationBarBackground和a组成UINavigationItemView.还有其他两种方法可以让它发挥作用.
在索引1而不是0 处插入自定义图像视图.这使其在按住按钮下方时显示在本机背景图像上方.
使用iOS 5的UIAppearance协议.您可以为所有人设置背景图像
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]
或仅用于一个导航栏:
[navigationController.navigationBar setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]
在为纵向和横向开发iPhone应用程序时,请务必提供两个图像(UIBarMetricsDefault和UIBarMetricsLandscapePhone).
lud*_*ert 31
编辑:这已经过时了; 对于iOS5的有一个很大低于更好的答案,通过@Jenox.
导航条的完全自定义样式非常困难.我所知道的最好的文章是塞巴斯蒂安·塞利斯的这篇文章:http: //sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
这不会覆盖drawRect,并且包含一个很好的解释为什么这是一件好事.另请注意,您不必遵循他的教程.您可以在此处下载完整代码:https: //github.com/scelis/ExampleNavBarBackground
bha*_*inb 24
只是为了添加@Jenox给出的答案,如果你想同时支持iOS 4.xx和iOS 5.xx设备(即你的DeploymentTarget是4.xx),你必须小心将调用包装到外观代理中如果"外观"选择器存在与否,则在运行时检查.
你可以这样做:
//Customize the look of the UINavBar for iOS5 devices
if ([[UINavigationBar class]respondsToSelector:@selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
}
Run Code Online (Sandbox Code Playgroud)
您还应该保留可能已实施的iOS 4.xx解决方法.如果您已经实现了iOS 4.xx设备的'drawRect'解决方法,如@ludwigschubert所述,您应该将其保留在:
@implementation UINavigationBar (BackgroundImage)
//This overridden implementation will patch up the NavBar with a custom Image instead of the title
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
Run Code Online (Sandbox Code Playgroud)
这将使iOS 4和iOS 5设备中的NavBar外观相同.
| 归档时间: |
|
| 查看次数: |
79307 次 |
| 最近记录: |