简体   繁体   中英

Unable to add UIImageView Xcode Swift

For some reason I am unable to add a UIImageView to my app. This is the code I am using and I have searched for quite a while to figure this out but haven't had any luck.

    super.viewDidLoad()

    let cloudimage = UIImage(named: "cloud")
    let cloudView = UIImageView(image: cloudimage)
    self.view.addSubview(cloudView)
    cloudView.frame = CGRectMake(0,0,100,200)

The image is a .png in my assets folder so I don't think it's that. I do have auto layout settings enabled if that is an issue? I know it can be an issue with moving a UIImageView around by using its frame, but I think I should still be able to place the image in the View no problem with this code.

I am not quite sure what to do any suggestions would be great, this is extremely frustrating.

you need to add UIimageview to main view.

self.view.addSubview(cloudView)

Try by giving the image extension also while setting to UIImage

    let cloudimage = UIImage(named: "cloud.png")
    let cloudView = UIImageView(image: cloudimage)
    cloudView.frame = CGRectMake(0,0,100,200)
    self.view.addSubview(cloudView)

Checked and its working.

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