简体   繁体   English

iPad UINavigationController自定义栏背景图像

[英]iPad UINavigationController Custom Bar Background Image

I am trying to create a UINavigationController with a background Image... 我正在尝试使用背景图像创建UINavigationController ...

I have the following code at the top of the class where I implement the 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

Then, inside my @implementation of my controller in the "viewDidLoad" function, I have the following... 然后,在“ viewDidLoad”函数的控制器的@implementation中,我具有以下内容...

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

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

I know I am close, because everything is working almost perfectly, except the image is 150px in height, and it's being squished down to a smaller size (doing a log of self.frame.size.height is giving me 44.0000) but is pushed down a certain number of pixels from the top... 我知道我已经接近了,因为除图像高度为150px之外,其他所有东西都几乎可以正常运行,并且图像被压缩为较小的尺寸(对self.frame.size.height的记录为44.0000),但是被推送了从顶部向下一定数量的像素...

I know I am close, but if anyone could help me out, it would be muchly appreciated. 我知道我很亲近,但是如果有人可以帮助我,将不胜感激。

Thank you, 谢谢,

--d --D

You really need to actually make your image the same size as your navigation bar. 您实际上确实需要使图像的大小与导航栏的大小相同。 Don't try to do this unless you really have art that was made for a UINavigationBar . 除非您确实拥有 UINavigationBar 制作的艺术品,否则不要尝试这样做。

This code will work, so long as the dimensions are correct: 只要尺寸正确,此代码将起作用:

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

To support different orientations with differently-sized navigation bars, just send -[UIApplication statusBarOrientation] (I've heard from a lot of people that -[UIDevice orientation] doesn't work as expected, but I haven't tried it). 要使用大小不同的导航栏支持不同的方向,只需发送-[UIApplication statusBarOrientation] (我听说很多人都认为-[UIDevice orientation]不能按预期工作,但我没有尝试过)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM