tags:

views:

11402

answers:

5

How can I create basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, a three UIButton will create dynamically and set its layout or properties.

+6  A: 

To add a button programatically to your controller's view, use the following:

-(void)viewDidLoad
{
   UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   btn.frame = CGRectMake(0, 0, 100, 50);
   [btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
   [self.view addSubview:btn];
}

To add three of these, rinse and repeat.

Jason
+20  A: 

Here's one:

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
mahboudz
Thanks! can you explain to me what is aMethod?
sasayins
aMethod is your own defined method which will respond to the button press. (For me)It takes an id object.
John Smith
When the button is manipulated (pressed and held down, or pressed and released or dragged through, etc) you want something to happen, right? The code that you want called is usually a method like:- (void) myButtonWasPressed or - (void) myButtonWasPressed:(id) whichButtonThat's what I am talking about.
mahboudz
The aMethod is kinda like and IBaction that is called when the button is tapped.
David
+1  A: 

are you trying to create three dynamic instances of a button? if not, then the answer is mahboudz above.

otherwise, you can just put the creator instance within a loop and dynamically add names from an array if you so wish.

Adam Libonatti-Roche
yup i tried mahboudz, and at the same time, i tried the looping. thanks
sasayins
+4  A: 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addMyButton];   // Call add button method on view load
}

- (void)addMyButton{    // Method for creating button, with background image and other properties

    UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
    [playButton setTitle:@"Play" forState:UIControlStateNormal];
    playButton.backgroundColor = [UIColor clearColor];
    [playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
    UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
    UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
    UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
    [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];  
}
Khawar
great thanks, can you arrange the code? thanks =)
sasayins
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
Khawar
+2  A: 

All well and good my friends, except none of the answers address 1 key issue - what if you want to set a background color on the button? Not so straight forward!

Changing this line:

playButton.backgroundColor = [UIColor clearColor];

...to this:

playButton.backgroundColor = [UIColor redColor];

Well, some interesting results. Apparently this only sets the background for the frame that the button is contained within. So you'll see that for a rounded rectangle button would display the colors ONLY in the corners! Why i do not know.

I've checked the docs, nothing. If anybody could shed some light on what should be a trivial issue it would be much appreciated. Thanks

OOP_Master
wow. thanks. i never thought of some color manipulation of the button. thanks!
sasayins
if you want to change the background color of a UIButton you have to use UIButtonTypeCustom type instead of UIButtonTypeRoundedRect
cpjolicoeur