UINavigationBar设置背景图像中心swift ..?

时间:2017-02-27 22:23:05

标签: ios iphone swift xcode

我将背景图片放在此处:

self.navigationController.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)

但是,重复图像。不,我想让它重复给我并把它放在中心..?

2 个答案:

答案 0 :(得分:4)

以下是使用UIAppearance和UIImage API修复它的方法:

// disable image stretching by defining left and top caps. 
let navbarImage = image.stretchableImage(withLeftCapWidth: 1, topCapHeight: 1)
UINavigationBar.appearance().setBackgroundImage(navbarImage, for: .default)

之前的例子: enter image description here

并在修复之后: enter image description here

答案 1 :(得分:0)

尝试使用适合我的method以下。

        var headerview = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 44))            
        let imgview = UIImageView(frame: CGRect(x: 75, y: 0, width: 150, height: 44))
        imgview.image = UIImage(named: "ImageName")
        imgview.contentMode = UIViewContentMode.scaleAspectFit
        headerview.addSubview(imgview)
        self.navigationController?.navigationBar.topItem?.titleView = headerview