How do you create a UIImage View Programmatically - Swift

前端 未结 8 1689
梦如初夏
梦如初夏 2020-12-02 04:23

I\'m trying to create a UIImage View programmatically, I have a new view and I tried doing this

let imageName = \"yourImage.png\"
yourview.backgroundColor =          


        
8条回答
  •  长情又很酷
    2020-12-02 05:19

    First you create a UIImage from your image file, then create a UIImageView from that:

    let imageName = "yourImage.png"
    let image = UIImage(named: imageName)
    let imageView = UIImageView(image: image!)
    

    Finally you'll need to give imageView a frame and add it your view for it to be visible:

    imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
    view.addSubview(imageView)
    

提交回复
热议问题