I'm practicing beginner code since I'm new and I just have run into a whole lot of confusion here... this is what I have so far
UIView *catView = [[UIView alloc] init];
UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[catView.view addSubview:imageView];
I don't understand what and why something in here is wrong, can someone help?
To create a new UIImageView programmatically in Swift, we need to create a new instance of UIImageView class and then set UIImage to an image property. Like so: The code snippet above creates a new instance of UIImageView and adds an image to it. However, it is not enough to make an image display.
Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.
A view object renders content within its bounds rectangle and handles any interactions with that content. The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.
An object that manages the content for a rectangular area on the screen. Views are the fundamental building blocks of your app's user interface, and the UIView class defines the behaviors that are common to all views. A view object renders content within its bounds rectangle, and handles any interactions with that content.
//You need to specify the frame of the view
UIView *catView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,400)];
UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//specify the frame of the imageView in the superview , here it will fill the superview
imageView.frame = catView.bounds;
// add the imageview to the superview
[catView addSubview:imageView];
//add the view to the main view
[self.view addSubview:catView];
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