简体   繁体   中英

In xcode 11.4.1 I get this error “Use of unresolved identifier” for an image which is in assets folder, why is that?

import UIKit

class ViewController: UIViewController {

    let canoaimageview: UIImageView = {
        let imageview = UIImageView(image: canoa) //ERROR Use of unresolved identifier 'canoa'

        imageview.translatesAutoresizingMaskIntoConstraints = false
       return imageview
}()

  override func viewDidLoad() {
          super.viewDidLoad()


    view.addSubview(canoaimageview)
    setupLayout()
    }

    private func setupLayout() {
    canoaimageview.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    canoaimageview.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
    canoaimageview.widthAnchor.constraint(equalToConstant: 150).isActive = true
    canoaimageview.heightAnchor.constraint(equalToConstant: 150).isActive = true
    }


}

//an attached image is included showing the error and assets folder

在此处输入图像描述

You need to make an UIImage first to assign it to imageView like this

    let canoaimageview: UIImageView = {
        let imageview = UIImageView(image: UIImage(named: "canoa")) //ERROR Use of unresolved identifier 'canoa'
        imageview.contentMode = .scaleAspectFit
        imageview.translatesAutoresizingMaskIntoConstraints = false
       return imageview
}()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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