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

Danny Swan picture Danny Swan · Jul 9, 2013 · Viewed 33.3k times · Source

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?

Answer

soryngod picture soryngod · Jul 9, 2013
//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];