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

Jos*_*var 2 iphone xcode ios swift

我将背景图像放置为:

self.navigationController.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
Run Code Online (Sandbox Code Playgroud)

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

Kar*_*tor 5

以下是使用 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)
Run Code Online (Sandbox Code Playgroud)

之前的例子: 在此处输入图片说明

修复后: 在此处输入图片说明