UINavigationBar iPhone 6/6的自定义图像+

问题描述:

我有一个由设计人员为UINavigationBar制作的自定义图像。我为iPhone获得640x128 & 640x88尺寸切片。我的问题:UINavigationBar iPhone 6/6的自定义图像+

  1. 如果我隐藏状态栏,我使用640x88大小或我仍然需要使用640x128,

  2. 对于iPhone6分之6+也iPad的,我不知道知道如何重新使用这些图像,因为导航栏的长宽比&的大小不同。

仅供参考,图像是使用颜色从上到下(在Y轴)的渐变色。即使我使用可伸缩的UIImage,我如何解决每个设备上不同高度的导航栏问题?我需要针对上面的iOS 7 &。

你可以试试这个 在头部,你可以定义宏不同的设备,如

#define IS_IPHONE_5 (fabs((double)[ [ UIScreen mainScreen ] bounds ].size.height - (double)568) < DBL_EPSILON) 
#define IS_IPHONE_6 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)667) < DBL_EPSILON) 
#define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)736) < DBL_EPSILON) 

然后if和else条件的..

UIImageView *imageview; 
if (IS_IPHONE_5){ 
    imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)]; 
    imageview.image=[UIImage imageNamed:@"yourimage" ]; 
     [self.navigationController.navigationBar addSubview:imageview];} 
else if(IS_IPHONE_6){ 
    imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 55)]; 
    imageview.image=[UIImage imageNamed:@"select_albums6"]; 
}else if (IS_IPHONE_6_PLUS){ 
    imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 414, 55)]; 
    imageview.image=[UIImage imageNamed:@"select_albums6"]; 
} else 
{ 
     // 3.5 inch 
    imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)]; 
    imageview.image=[UIImage imageNamed:@"select_album"]; 
} 

,然后添加导航子视图

 [self.navigationController.navigationBar addSubview:imageview]; 

注意:这个代码写在ViewDidLoad和ViewWillAppear方法,所以导航栏设置为您的自定义尺寸... 我希望这会帮助

+0

您也应该检查其iPhone或iPad的成语,比如: '#定义IS_IPHONE(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone)' – Laszlo 2014-10-17 12:56:23

+0

@Tuss你是对的 – 2014-10-17 12:58:20

为了解决这个问题,我只是为iPhone 4/5/5创建三个不同的图像,iPhone 6和iPhone 6 Plus。 的图像分辨率为320x64,375x64 @ 2X,414x64 @×(高度为64,因为我需要的背景图像导航栏+状态栏)

然后我就应用初始化应用正确的图像:

NSString * navBarImageName; 

if (IS_IPHONE_6P) { 
    navBarImageName = @"navBarBackground_iPhone6Plus"; 
} else if (IS_IPHONE_6) { 
    navBarImageName = @"navBarBackground_iPhone6"; 
} else { 
    navBarImageName = @"navBarBackground"; 
} 

UIImage * navBarBackground = [UIImage imageNamed:navBarImageName]; 
[[UINavigationBar appearance] setBackgroundImage:navBarBackground forBarMetrics:UIBarMetricsDefault]; 

为NavigationBar背景添加自定义图像时,首先应该考虑的是自定义图像的尺寸。

1)320x44 => background.png
2)640x88 => [email protected]
3)1334x183 => [email protected]

使用下面的代码添加背景图片,并避免在导航栏的背景图像中出现任何平铺。

[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];