iPad UINavigationController 自定义栏背景图像
我正在尝试创建一个带有背景图像的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实需要使图像与导航栏的大小相同。除非您确实拥有为
UINavigationBar
制作的艺术作品,否则不要尝试这样做。只要尺寸正确,此代码就可以工作:
要支持具有不同大小的导航栏的不同方向,只需发送
-[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:
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).