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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Peter Sepassi](https://secure.gravatar.com/avatar/531d8fbc8d3ea18dedc05e480173951e?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Peter Sepassi
3,180 PointsUIButton Programmatically
I can't figure out how to add a UIButton programmatically onto the CrystalBall app. The UIImageView works. Here is my code:
UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];
[self.view insertSubview:imageView atIndex:0];
UIImage *buttonOff = [UIImage imageNamed:@"button_bg_off.png"];
//UIImage *buttonOn = [UIImage imageNamed:@"button_bg_on.png"];
UIButton *predictButton = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 485.0, 280.0, 44.0)];
predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
[predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
[predictButton setTitle:@"Predict" forState:UIControlStateNormal];
[predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:predictButton];
1 Answer
![Amit Bijlani](https://uploads.teamtreehouse.com/production/profile-photos/5393/micro_NewProfile2.jpg)
Amit Bijlani
Treehouse Guest TeacherYour last line of code should be adding the predictButton
to self.view
[self.view addSubview:predictButton];
Marc Maycas
Python Web Development Techdegree Student 26,884 PointsMarc Maycas
Python Web Development Techdegree Student 26,884 PointsI was also trying to solve this extra credit. However I got stuck with it.
I corrected my code following the guidelines that Peter proposed as well as changing the last line to what Amit proposed.
Unfortunately my view appears without a button. It's like if having deleted the button deletes it from the view...
Here's my code which is almost exactly as Peter's:
//Adding the button programmatically UIImage *buttonOff = [UIImage imageNamed:@"button_bg_off.png"]; //UIImage *buttonOn = [UIImage imageNamed:@"button_bg_on.png"];
Marc Maycas
Python Web Development Techdegree Student 26,884 PointsMarc Maycas
Python Web Development Techdegree Student 26,884 PointsShame on me... I have seen that the button was out of the screen, placing it in:
predictButton.frame = CGRectMake(80.0, 380.0, 160.0, 80.0);
Makes it work!