我正在尝试创建一个带有背景图像的 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/