【问题标题】:UINavigationBar custom image for iPhone 6/6+适用于 iPhone 6/6+ 的 UINavigationBar 自定义图像
【发布时间】:2014-12-12 14:58:25
【问题描述】:

我有一个由 UINavigationBar 设计师制作的自定义图像。我得到了适用于 iPhone 的 640x128 和 640x88 大小的切片。我的问题:

  1. 如果隐藏状态栏,是使用640x88大小还是还是需要使用640x128,

  2. 对于 iPhone 6/6+ 和 iPad,我不知道如何重复使用这些图像,因为导航栏的大小和纵横比不同。

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

【问题讨论】:

    标签: ios ios7 uinavigationcontroller uinavigationbar iphone-6-plus


    【解决方案1】:

    你可以试试这个 在标题中,您可以为不同的设备定义宏,例如

    #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 方法中,因此导航栏设置为您的自定义大小... 我希望这会有所帮助

    【讨论】:

    • 您还应该检查它是否是 iPhone 或 iPad 成语,例如:#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    【解决方案2】:

    为了解决这个问题,我只为 iPhone 4/5/5s、iPhone 6 和 iPhone 6 Plus 创建了三个不同的图像。 图片分辨率为320x64、375x64@2x、414x64@3x(高度为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];
    

    【讨论】:

      【解决方案3】:

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

      1) 320x44 => background.png
      2) 640x88 => background@2x .png
      3) 1334x183 => background@3x.png

      使用以下代码添加背景图片,避免导航栏背景图片平铺。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 2016-01-03
        相关资源
        最近更新 更多