uiimageview未在视图控制器中显示图像

时间:2018-12-23 21:49:58

标签: xcode swift3 uiimageview uiimage

我正在尝试在视图控制器中显示图表图像。我的资产中有一个名为“ powerchart”的图像,但是无法在视图控制器中显示它。我正在尝试以编程方式构建应用程序,因此不使用情节提要。下面的代码。我试过使用UIImage的几种组合。功能,但无法加载图像。任何帮助将不胜感激

import Foundation
import UIKit

class MarketViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.yellow

        navigationController?.title = "Market"


        view.addSubview(marketChart)
        setupMarketScreen()

    }

//    create chart
    let marketChart: UIImageView = {
        let chart = UIImageView()
        chart.image = UIImage.init(imageLiteralResourceName: "powerchart")
        return chart
    }()

    func setupMarketScreen() {

        marketChart.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive=true
        marketChart.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive=true
        marketChart.widthAnchor.constraint(equalToConstant: 500).isActive=true
        marketChart.heightAnchor.constraint(equalToConstant: 200).isActive=true

    }
}

1 个答案:

答案 0 :(得分:0)

以这种方式声明。下面的代码对我来说很好。

import UIKit

class ViewController: UIViewController {
    /*
    let marketChart: UIImageView = {
        let chart = UIImageView()
        chart.image = UIImage.init(imageLiteralResourceName: "backimg3.jpeg")
        return chart
    }() */
    let marketChart:UIImageView = UIImageView.init(image: UIImage (named: "backimg3.jpeg"))

    override func viewDidLoad() {
        super.viewDidLoad()
           self.view.addSubview(marketChart)
    }


}