Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将导航栏标题更改为图像?其他方法没有奏效_Ios_Swift_Image_Uinavigationbar_Title - Fatal编程技术网

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

Ios 如何将导航栏标题更改为图像?其他方法没有奏效,ios,swift,image,uinavigationbar,title,Ios,Swift,Image,Uinavigationbar,Title,我已经尝试了下面的代码以及Stackoverflow上多个其他问题的答案,但我仍然无法实现这一点。。我知道图像配置文件存在于我的资产文件夹中。我已经从我的导航栏和导航项目,并尝试使用出口,但仍然没有得到什么。最多我在标题上的文字会消失…我觉得好像我错过了一些东西在一起 位于ViewController的ViewDidLoad函数中 let image = UIImage(named: "Profile") let imageView = UIImageView(image: im

我已经尝试了下面的代码以及Stackoverflow上多个其他问题的答案,但我仍然无法实现这一点。。我知道图像配置文件存在于我的资产文件夹中。我已经从我的导航栏和导航项目,并尝试使用出口,但仍然没有得到什么。最多我在标题上的文字会消失…我觉得好像我错过了一些东西在一起

位于ViewController的ViewDidLoad函数中

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

谢谢你的帮助

最初为imageview设置帧

 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
在目标C中:

self.navigationItem.titleView=[[UIImageView alloc]initWithImage:image]


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

您的图像大小是多少