简体   繁体   English

UINavigationBar设置背景图像中心迅速。

[英]UINavigationBar set background image center swift..?

I'm placing the background image this: 我要放置背景图片:

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

but, repeat the image. 但是,重复图像。 No I want it to be repeated to me and Put it center.. ? 不,我希望对我重复并将其放在中心。

Here's how to fix it using UIAppearance and UIImage API: 以下是使用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)

Example before: 之前的示例: 在此处输入图片说明

and after the fix: 修复后: 在此处输入图片说明

Try with below method which worked for me. 请尝试以下对我有用的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM