微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

屏幕上没有出现图像视图

如何解决屏幕上没有出现图像视图

我正在尝试将背景图像添加到我的屏幕上并设置一些约束,但是不幸的是,该图像视图没有出现,我也不知道是什么原因。我检查了图像文件是否在项目文件夹中。

import UIKit

class ViewController: UIViewController {

    let backgroundImageView = UIImageView()

    override func viewDidLoad() {
       super.viewDidLoad()
       setBackground()
    }

    func setBackground() {
        view.addSubview(backgroundImageView)
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        backgroundImageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        backgroundImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        backgroundImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        backgroundImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        backgroundImageView.image = UIImage(named: "background-test-image")
    }

}

解决方法

将图像放入Assets.xcassets文件夹中

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。