如何将导航栏标题更改为图像?其他方法没有奏效

How can you change the navigation bar title to an image? Other methods have not worked

我已经尝试了下面的代码和 Whosebug 上其他多个问题的答案,但我仍然无法正常工作。我知道图像配置文件存在于我的资产文件夹中。我已经从我的导航栏和导航项中创建了出口并尝试使用它但仍然一无所获。顶多我标题上的文字会消失。。。总感觉少了点什么。

位于 ViewController 的 ViewDidLoad 函数中

    let image = UIImage(named: "Profile")
    let imageView = UIImageView(image: image)
    self.navigationItem.titleView = imageView
    self.navigationItem.titleView?.sizeToFit()

感谢您的帮助!

初始设置图像视图的框架

 let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
 imageView.contentMode = .ScaleAspectFit

 if  let image = UIImage(named: "Profile")
{
 imageView.image = image

 }else
  {
   imageView.image =  UIImage(named: "Profile.png")
   }
   self.navigationItem.titleView = imageView

在Objective C中:

self.navigationItem.titleView = [[UIImageView 分配] initWithImage:image];

其中 "image" 是您要设置为标题视图的图像实例。