【问题标题】:How to set background image for UINavigationBar on different iOS Devices如何在不同的 iOS 设备上为 UINavigationBar 设置背景图片
【发布时间】:2017-09-22 00:08:53
【问题描述】:

我想在UINavigationBar 上设置完整图像,为此我有:

@2x image (640 x 128)
@3x image (960 x 192)

下面的截图是问题:

请参考这个黄色轮廓。这部分正在切割。

我已经写了这段代码来添加图片:

 override func viewDidLoad() {
        super.viewDidLoad()                   
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"nav-bar-b"),for: .any, barMetrics: .default)
        }

请帮助我提供更好的解决方案。

【问题讨论】:

  • iPhone 6 使用@2x 图像资源而不是 R4 或其他东西,因为苹果没有为背景图像提供合适的 API!现在最直接的方法是创建 2 个资产并以编程方式加载它们
  • 将您的代码更改为 viewdidappear 并检查一次
  • @Anbu.Karthik 我试过用这个没用。
  • 为 iphone 6 添加分辨率为 750x128 的图像。
  • @KKRocks 在下面发布您的相同答案。

标签: ios swift3 uinavigationcontroller ios10.2


【解决方案1】:

我已经这样解决了这个问题:-

根据设备大小获取导航图像,否则会破坏导航图像。

iPhone 6P => //1242 × 191 像素
iPhone 6 = > //750 × 128 像素
iPhone 5 = > //640 × 128 像素

func SetNavigationImage()
    {
        var navBackgroundImage:UIImage!

        if IS_IPHONE_6P
        {
            navBackgroundImage = UIImage(named: "nav-bar-b_1242×191") //1242 × 191 pixels
        }else if IS_IPHONE_6
        {
            navBackgroundImage = UIImage(named: "nav-bar-b_750×128")//750 × 128 pixels
        }
        else
        {
            navBackgroundImage = UIImage(named: "nav-bar-b_640×128")//640 × 128 pixels
        }
        UITabBar.appearance().layer.borderWidth = 0.0
        UITabBar.appearance().clipsToBounds = true
        UINavigationBar.appearance().isTranslucent = false
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
        UINavigationBar.appearance().setBackgroundImage(navBackgroundImage, for:.default)
        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().tintColor = .white
    }


var IS_IPHONE_4_OR_LESS =  UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
var IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
var IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
var IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0

【讨论】:

  • 我正在寻找更好的解决方案。 :(
  • 根据我的研究,如果您的导航图像在中间作为徽标或文本,那么我会得到此解决方案,其他解决方案不适合我 :)
【解决方案2】:

在 viewDidLoad 中试试这个代码

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "image")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)

【讨论】:

  • 您的代码对我不起作用。 :( 我正在使用 self.navigationonteoller.navigationBar
【解决方案3】:

1.self.navigationController.navigationBar.setBackgroundImage(图像, forBarMetrics: .Default)

2.导航栏高度由44点(88像素)改为64点(128像素)。

【讨论】:

  • 我已经尝试使用您提供的代码。它不起作用:(@Femina Brahmbhatt
【解决方案4】:

创建可调整大小的图像。因此,图像将被垂直和水平缩放以适应包括状态栏在内的导航栏。

对于 Swift 3:

if let image = UIImage(named: "imagefile") {
   let backgroundImage = image.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch)
   self.navigationController?.navigationBar.setBackgroundImage(backgroundImage, for: .default)
}

if let image = UIImage(named: "imagefile") {
   let backgroundImage = image.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch)
   UINavigationBar.appearance().setBackgroundImage(backgroundImage, for: .default)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 2014-08-09
    • 2021-07-12
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多