在横向模式下的自定义背景UINavigationBar的
-
22-08-2019 - |
题
我加入了我的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];
解决方案
您可能需要设置你的背景图像视图的autoresizingMask;尝试使用UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
其他提示
在两个屏幕取向模式是好得多使用
[navigationController.navigationBar insertSubview:imgView atIndex:0];
此使图像视图下的所有其他视图和所有默认导航栏元素(标题,标准按钮)工作行。
在一些研究和试错,我发现周围的工作,当你进入电影播放模式不会取代导航栏。 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
奔给的溶液确实解决了问题,但它在横向模式下拉伸图像。我结束了创建为肖像模式下的两个图像 - 一个用于景观等。然后我在shouldAutoRotate添加代码以改变基于所述取向导航栏图像
可以通过检查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);
}
和这个完整的演示的Xcode计划对定制UINavigationBar的外观可能会有所帮助。它还包括@ 2×版本背景图像,以支持在iPhone 4设备视网膜显示器。
尝试[UIImage imageNamed:@"navbar_landscape.png"]
的更简单的方法,因为UI元素正是的 imageNamed:强>适用于,如ljonesATL显示