iphone - iPad UINavigationController 自定义栏背景图像

标签 iphone ipad uinavigationcontroller uinavigationbar

我正在尝试创建一个带有背景图像的 UINavigationController...

我在实现 UINavigationController 的类顶部有以下代码。

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:@"header.jpg"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];
}

@end

然后,在“viewDidLoad”函数中我的 Controller 的 @implementation 中,我有以下内容...

MainViewController *controller = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
self.navController = nav;
[nav release];

我知道我已经很接近了,因为一切都工作得几乎完美,除了图像的高度是 150px,并且它被压缩到更小的尺寸(对 self.frame.size.height 进行记录给我 44.0000)但从顶部向下推了一定数量的像素...

我知道我已经很接近了,但如果有人可以帮助我,我将不胜感激。

谢谢,

--d

最佳答案

您确实需要使图像与导航栏的大小相同。除非您确实有UINavigationBar 制作的艺术作品,否则不要尝试这样做。

只要尺寸正确,此代码就可以工作:

- (void)drawRect:(CGRect)rect {
  [img drawInRect:rect];
}

要支持不同方向和不同大小的导航栏,只需发送 -[UIApplication statusBarOrientation] (我从很多人那里听说 -[UIDeviceorientation] 没有按预期工作,但我没有尝试过)。

关于iphone - iPad UINavigationController 自定义栏背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3223639/

相关文章:

iphone - 什么是首选: implement method with GCD inside and then just simple call,或Implement方法,然后在以后使用GCD调用它?

iphone - 如何在不点击导航栏的情况下突出显示导航栏中的 BarButton?

ios - 按钮的导航栏项目拉伸(stretch)图像

iphone - 调整 iPhone 上 UINavigationController 一部分的 viewController View 的大小

ios - UNCalendarNotificationTrigger 每小时触发器返回错误的触发日期

iphone - cocos2d中如何停止runAction

iphone - 使用 cornerradius IOS 使其变圆时发出相关的 UIimageView

ios - 安装应用程序时,请在选项字典中包含 kCFBundleIdentifierKey

ios - UINavigationController 交互式弹出手势仅在第一次有效

iphone - 在 iPhone 应用程序中访问 Settings.bundle 的 Child.plist - 如何?