我想在我的导航栏中添加图像背景。
是吗?
//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];
最佳答案
这是来自 link 的代码@luvieere 提到。
将此代码粘贴到上面的 Root View Controller 中
@implementation rootviewController
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
从 iOS 5 开始,有一种官方方法可以做到这一点。 (参见 iOS Developer Library)
// someplace where you create the UINavigationController
if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
[navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
但是,除非您真的想放弃 iOS 4 及更低版本,否则请保留旧代码以实现向后兼容性。
关于ios - 如何在 iPhone 导航栏上添加背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1692487/