Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!