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

Peter Sepassi
Peter Sepassi
3,180 Points

UIButton 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
STAFF
Amit Bijlani
Treehouse Guest Teacher

Your last line of code should be adding the predictButton to self.view

[self.view addSubview:predictButton];
Marc Maycas
seal-mask
.a{fill-rule:evenodd;}techdegree
Marc Maycas
Python Web Development Techdegree Student 26,884 Points

I 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"];

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];
[self.view addSubview:predictButton];
Marc Maycas
seal-mask
.a{fill-rule:evenodd;}techdegree
Marc Maycas
Python Web Development Techdegree Student 26,884 Points

Shame 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!