如何在不同的iOS设备上为UINavigationBar设置背景图像

时间:2017-04-25 06:06:24

标签: ios swift3 uinavigationcontroller ios10.2

我想在Form_Activate()上设置完整图片,为此我有:

UINavigationBar

屏幕截图下方是问题:

enter image description here

请参阅此黄色轮廓。这部分正在削减。

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

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

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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在viewDidLoad中尝试此代码

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

答案 2 :(得分:0)

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

2.导航栏的高度从44点(88像素)变为64点(128像素)。

答案 3 :(得分:0)

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

对于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)
}