2008-12-19 34 views
7

我为我的UINavigationBar添加了自定义背景。只要手机处于肖像模式,它就可以正常工作。当我切换到横向模式时,一半条显示为蓝色(默认导航栏颜色),一半显示为我的图像横向模式下UINavigationBar的自定义背景

如何拉伸横向模式的图像并使纵向模式再次变小?

感谢

解决方案
柜面的人正在寻找一个答案,如何将图像添加到导航条 - 在这里不用

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 44.0)]; 
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"navbar_landscape" ofType:@"png"]]]; 
[navigationController.navigationBar addSubview:imgView]; 
[imgView release]; 
+0

我不得不将第二行更改为: [imgView setImage:[UIImage imageNamed:@“navbar.png”]]; ...如果任何人与noshow挣扎。 – maralbjo 2010-10-21 09:54:37

+0

谢谢,嗯帖子有点老了,反正..怎么样改变navigationBar的高度?我应该在这种情况下继承吗? – aneuryzm 2011-06-19 08:47:50

回答

3

你可能需要设置的autoresizingMask你的背景图像视图;尝试使用UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

1

Ben给出的解决方案确实解决了这个问题,但它在横向模式下拉伸了图像。我最终创建了两个图像 - 一个用于风景,另一个用于肖像模式。然后我在shouldAutoRotate添加的代码基于取向

6

在这两种屏幕方向模式改变导航栏图像这是更好的使用

[navigationController.navigationBar insertSubview:imgView atIndex:0]; 

这使得在所有其他的意见和所有默认的导航栏图像视图元素(标题,标准按钮)工作正常。

6

经过一些研究和追踪和错误,我找到了一种解决方法,当您进入电影播放模式时,它不会取代导航栏。 Hopeefully这不会导致应用程序审批的问题,而是基于对这个职位,我认为它应该是罚款:

http://developer.apple.com/iphone/library/qa/qa2009/qa1637.html

@implementation UINavigationBar (UINavigationBarCategory) 

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 
    if([self isMemberOfClass: [UINavigationBar class]]){ 
     UIImage *image = [UIImage imageNamed:@"navBarBackground.png"]; 
     CGContextClip(ctx); 
     CGContextTranslateCTM(ctx, 0, image.size.height); 
     CGContextScaleCTM(ctx, 1.0, -1.0); 
     CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 
    }else{ 
     [super drawLayer:layer inContext:ctx]; 
    } 
} 

@end 
0

尝试的[UIImage imageNamed:@"navbar_landscape.png"]更简单的方法,因为UI元素到底是什么imageNamed :适用于,如ljonesATL所示。

1

您可以通过检查UINavigation栏实例的帧大小提供不同方向的不同的图像改变为纵向和横向方向的图像:

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context 
{ 
    if ([self isMemberOfClass:[UINavigationBar class]] == NO) { 
     return; 
    } 

    UIImage *image = (self.frame.size.width > 320) ? 
         [UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait]; 
    CGContextClip(context); 
    CGContextTranslateCTM(context, 0, image.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 
} 
上定制UINavigationBar的外观可能是

而且this complete demo Xcode project很有帮助。它还包括@ 2x版本的背景图像,以支持iPhone 4设备上的视网膜显示。

相关问题