ios - 如何在 iPhone 导航栏上添加背景图片?

标签 ios iphone cocoa-touch background uinavigationbar

我想在我的导航栏中添加图像背景。

是吗?

//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/

相关文章:

ios - 我可以使用父类(super class)推送到同样继承的新 View Controller 吗?

iOS - 自定义节标题边界问题

ios - 是否可以为第一个字符手动创建 UITextRange?

iPhone 应用程序首选项

iphone - 从 iPhone 应用程序打开 Safari 的指南

ios - 禁用与 UIBarButtonItem 的用户交互

ios - XCode 将重复和不正确的图标文件名添加到 Info.plist

ios - 将 UINavigationController 添加到现有 UIViewController 的正确方法

苹果手机 : How to display Activity Indicator on clicking Tab Bar?

objective-c - iPhone 应用程序上的 JSON 错误