Swift ios如何设置导航栏图像不拉伸

Swift ios如何设置导航栏图像不拉伸,ios,swift,navbar,Ios,Swift,Navbar,我已经可以使用ff代码将背景图像添加到导航栏,但图像被拉伸。有没有办法不被拉伸 self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "BG.jpg"), for: .default) 你可以试试这个 UINavigationBar.appearance().setBackgroundImage(UIImage(named: "BG.jpg")!.resizableImage(withCapIns

我已经可以使用ff代码将背景图像添加到导航栏,但图像被拉伸。有没有办法不被拉伸

 self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "BG.jpg"), for: .default)
你可以试试这个

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "BG.jpg")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
也可以尝试代码中的其他选项

更新

您甚至可以在以下类别中尝试
UINavigationBar

extension UINavigationController {
    func setBackgroundImage(_ image: UIImage) {
        navigationBar.isTranslucent = true
        navigationBar.barStyle = .blackTranslucent

        let imageView = UIImageView(image: image)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.translatesAutoresizingMaskIntoConstraints = false

        view.insertSubview(imageView, belowSubview: navigationBar)
        NSLayoutConstraint.activate([
            imageView.leftAnchor.constraint(equalTo: view.leftAnchor),
            imageView.rightAnchor.constraint(equalTo: view.rightAnchor),
            imageView.topAnchor.constraint(equalTo: view.topAnchor),
            imageView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor)
        ])
    }
}
根据您的最终要求,上述代码可能会有所不同。

您可以尝试此功能

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "BG.jpg")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
class TutorialViewController: UIViewController {

// This property will set the Image to Navigation Bar
public var navigationBarImage: UIImage? {
    get {
        return (navigationItem.titleView as? UIImageView)?.image
    } set {
        if newValue != nil {
            let imageView = UIImageView(image: newValue)
            imageView.contentMode = .scaleAspectFill
            navigationItem.titleView = imageView
        } else {
            navigationItem.titleView = nil
        }
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    navigationBarImage = #imageLiteral(resourceName: "my_nav_image")
}
也可以尝试代码中的其他选项

更新

您甚至可以在以下类别中尝试
UINavigationBar

extension UINavigationController {
    func setBackgroundImage(_ image: UIImage) {
        navigationBar.isTranslucent = true
        navigationBar.barStyle = .blackTranslucent

        let imageView = UIImageView(image: image)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.translatesAutoresizingMaskIntoConstraints = false

        view.insertSubview(imageView, belowSubview: navigationBar)
        NSLayoutConstraint.activate([
            imageView.leftAnchor.constraint(equalTo: view.leftAnchor),
            imageView.rightAnchor.constraint(equalTo: view.rightAnchor),
            imageView.topAnchor.constraint(equalTo: view.topAnchor),
            imageView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor)
        ])
    }
}

根据您的最终要求,上述代码可能会有所不同。

对于所有图像,您可以设置内容模式=

class TutorialViewController: UIViewController {

// This property will set the Image to Navigation Bar
public var navigationBarImage: UIImage? {
    get {
        return (navigationItem.titleView as? UIImageView)?.image
    } set {
        if newValue != nil {
            let imageView = UIImageView(image: newValue)
            imageView.contentMode = .scaleAspectFill
            navigationItem.titleView = imageView
        } else {
            navigationItem.titleView = nil
        }
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    navigationBarImage = #imageLiteral(resourceName: "my_nav_image")
}
.scaleToFill (UIViewContentModeScaleToFill)
.scaleAspectFit (UIViewContentModeScaleAspectFit)
.scaleAspectFill (UIViewContentModeScaleAspectFill) 
如果不希望看到边界之外的内容,则需要记住在包含的superview中将clipsToBounds设置为true

View.clipsToBounds = true

对于所有图像,可以设置内容模式=

.scaleToFill (UIViewContentModeScaleToFill)
.scaleAspectFit (UIViewContentModeScaleAspectFit)
.scaleAspectFill (UIViewContentModeScaleAspectFill) 
如果不希望看到边界之外的内容,则需要记住在包含的superview中将clipsToBounds设置为true

View.clipsToBounds = true

你想做什么?设置背景图像或标题视图?由于@Jatin的回答将帮助您设置标题设置背景图像,您可以分享导航栏图像的分辨率吗?您想做什么?设置背景图像或标题视图?@Jatin的回答将帮助您设置标题视图设置背景图像您可以分享导航栏图像的分辨率吗?您可以这样使用
navigationBarImage=UIImage.init(名为:“您的图像名称”)
您可以这样使用
navigationBarImage=UIImage.init(名为:“您的图像名称”)