Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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

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
如何在swift ios中使用导航栏标题中的图像_Ios_Swift_Uinavigationbar - Fatal编程技术网

如何在swift ios中使用导航栏标题中的图像

如何在swift ios中使用导航栏标题中的图像,ios,swift,uinavigationbar,Ios,Swift,Uinavigationbar,我有麻烦了。我想在导航栏标题中使用图像,但我得到一个错误,即“UIImage”不是“NSString”的子类型。下面的代码,看看它 class Dashboard: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = UIImage(named: "logo.png") // Do any additional s

我有麻烦了。我想在导航栏标题中使用图像,但我得到一个错误,即“UIImage”不是“NSString”的子类型。下面的代码,看看它

class Dashboard: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = UIImage(named: "logo.png")        
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

有人能建议吗?谢谢

设置navigationItem的标题视图

var image = UIImage(named: "logo.png")
self.navigationItem.titleView = UIImageView(image: image)
为我工作

        let image : UIImage = UIImage(named: "headerLogo")
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
    imageView.contentMode = .scaleAspectFit
    imageView.image = image
    navigationItem.titleView = imageView
Swift 5+,iOS 13+

navigationItem.titleView = UIImageView(image: UIImage(named: "id_logo"))

无法将UIImage类型的对象分配给NSString。navigationItem的标题只能保存文本数据(NSString)。