Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a UIImage View as a subView to an instance of UIView

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?

like image 528
Danny Swan Avatar asked Jul 09 '13 20:07

Danny Swan


People also ask

How to create a new UIImageView programmatically in Swift?

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.

How do I use an image as a background in UIView?

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.

What is the difference between UIView and a view 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.

What is UIView in Android?

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.


1 Answers

//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];
like image 166
soryngod Avatar answered Sep 20 '22 13:09

soryngod