PDA

View Full Version : Adding a UIButton to a view programatically




seventoes
Jul 23, 2008, 08:47 PM
Probably a noob question here, I'm still getting started with all of these cool new toys :D, but this doesnt seem to be working:

startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[startButton setTitle:@"Start Game" forState:UIControlStateNormal];
startButton.bounds = CGRectMake(120, 200, 80, 20);

[self addSubview:startButton];
Am i missing something simple?



firewood
Jul 23, 2008, 09:27 PM
Where are you calling this? In which view? Is that the view being displayed? Have you used the debugger to see if your code is getting called?

seventoes
Jul 23, 2008, 09:32 PM
Where are you calling this? In which view? Is that the view being displayed? Have you used the debugger to see if your code is getting called?

Doh. I'm dumb :(

Code wasnt even being called. Thanks for the little bump :P

Sbrocket
Jul 24, 2008, 12:30 AM
You don't want to set the bounds of your button like you are - you want the frame.

startButton.frame = CGRectMake(120, 200, 80, 20);

Read this page for more info on the distinction:
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/chapter_3_section_3.html

Keep in mind that 0,0 on the iPhone coordinate system is the top-left, not bottom-left (unlike is reflected in that article).

seventoes
Jul 24, 2008, 12:33 AM
You don't want to set the bounds of your button like you are - you want the frame.

startButton.frame = CGRectMake(120, 200, 80, 20);

Read this page for more info on the distinction:
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/chapter_3_section_3.html

Keep in mind that 0,0 on the iPhone coordinate system is the top-left, not bottom-left (unlike is reflected in that article).

Aha, i was wondering why the button liked to chill in the corner there, hehe.. Thanks for the tip!