iPad UINavigationController 自定义栏背景图像

发布于 2024-09-08 20:24:54 字数 882 浏览 5 评论 0原文

我正在尝试创建一个带有背景图像的 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”函数中我的控制器的 @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

I am trying to create a UINavigationController with a background Image...

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

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

I know I am close, but if anyone could help me out, it would be muchly appreciated.

Thank you,

--d

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-09-15 20:24:54

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

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

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

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

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文