When ever I set an image for a UIImageView in IB and simulate the interface the buttons and stuff show up but the image view doesn't.
You can display an image in a SwiftUI view by using the Image view. First you need to add the image to a new image set in your Assets. xcassets file in the Xcode project navigator.
Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.
image property will automatically handle that for you (when the new image is assigned). Where setting to nil is useful is where you want to explicitly mark the image as no longer in use, but do not want to remove / delete the UIImageView itself or set a new image. In those cases, setting . image = nil is a good idea.
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .
That's because the simulator used by Interface Builder is independent of your project. It's just taking your xib files, building the interface, and displaying it. As such, any resources it finds references to but can't find will be skipped. So if your UIImageView
uses an image named "myImage.png", then when the simulator runs, it's going to try to execute [UIImage imageNamed:@"myImage.png"];
. Since "myImage.png" is not part of the Simulator bundle, it will fail to create the image, and your UIImageView will be blank.
It can create buttons because (obviously) buttons are standard, globally available code. Your project-specific resources are not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With