Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App (iOS7) Designing your App Programming a Background Image

Jacob Samuel
PLUS
Jacob Samuel
Courses Plus Student 6,188 Points

Not sure why same code works in Xcode but doesn't work in challenges?

I am new to iOS. Can you please tell me what is wrong with the code below?

UIImage *image = [UIImage imageNamed:@"background"]; UIImageView *imageview = [[UIImageView alloc]initWithImage:image]; [self.view insertSubview:imageview atIndex:0];

This was my response to the challenge question.

2 Answers

Dale Rivera
Dale Rivera
17,106 Points

Hi Jacob.

It is important to note that simply initializing an image view with an image (via the initWithImage constructor) will set that image view's background to the specified image. This means that you have to do two things for this code challenge:

  1. Declare a UIImage object using the imageNamed constructor
UIImage *image = [UIImage imageNamed:@"background"];

2.Pass the UIImageView the newly declared UIImage using the initWithImage constructor

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

All together this gives you:

UIImage *image = [UIImage imageNamed:@"background"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];