How to set background image for UINavigationBar on different iOS Devices

前端 未结 4 1453
再見小時候
再見小時候 2021-01-24 19:37

I want to set full image on UINavigationBar, for this I have:

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

Below Screenshot is the

4条回答
  •  醉梦人生
    2021-01-24 19:46

    Create a resizeable image. So the image will be scaled vertically and horizontally to fit the navigation bar including the status bar.

    For Swift 3:

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

    or

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

提交回复
热议问题