【问题标题】:Navigation bar image for iphone 5 and 6 [duplicate]iphone 5和6的导航栏图像[重复]
【发布时间】:2015-12-29 10:56:38
【问题描述】:

我正在尝试为 iphone 5、6 和 6 plus 创建一个应用程序。

我为导航栏添加了自定义图像,但出现了问题。在 images.xcassets 中有两个插槽 2x 和 3x。

我已经知道 2x 适用于 iphone 5 和 6,而 3x 适用于 6 plus。如果我将 iphone 5 的图像放在 2x 插槽中,它会太小(当我在 iphone 6 上模拟应用程序时),如果我将手机 6 的图像放在该插槽中,它会太大(当我在 iphone 5 上模拟应用程序时) )。

那么,如果我的插槽不足,我该如何为 iphone 5 和 6 设置导航栏图像?我必须在那里使用条件吗?

【问题讨论】:

    标签: ios iphone swift xcasset


    【解决方案1】:

    你可以通过代码来处理它

    斯威夫特

    var navBarImage: UIImage? = nil
    if UIScreen.mainScreen().bounds().size.width == 375.0 {
        navBarImage = UIImage.imageNamed("yourimage-iphone6@2x").resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    }
    else {
        if UIScreen.mainScreen().bounds().size.width == 414.0 {
            navBarImage = UIImage.imageNamed("yourimage-iphone6plus3x").resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        else {
            navBarImage = UIImage.imageNamed("yourimage-iPhone5").resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        }
    }
    UINavigationBar.appearance().setBackgroundImage(navBarImage, forBarMetrics: UIBarMetricsDefault)
    

    目标c

    UIImage *navBarImage =nil;
    if ([[UIScreen mainScreen] bounds].size.width==375.0f) {
        navBarImage = [[UIImage imageNamed: @"yourimage-iphone6@2x"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    else if ([[UIScreen mainScreen] bounds].size.width==414.0f) {
        navBarImage = [[UIImage imageNamed: @"yourimage-iphone6plus3x"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    else{
       navBarImage = [[UIImage imageNamed: @"yourimage-iPhone5"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    }
    
    [[UINavigationBar appearance] setBackgroundImage:navBarImage forBarMetrics:UIBarMetricsDefault];
    

    【讨论】:

    • 另外,使用 switch 会让你的代码更加简洁和高效。
    • 感谢 Swift 版本,它可以工作!
    • WellCome @ArseniiNibble
    • @EricD。感谢您的建议,这对您有很大帮助
    【解决方案2】:
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      相关资源
      最近更新 更多