Tuesday, June 5, 2012

How do I create a basic UIButton programmatically?

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.

Source: Tips4all


  1. Here's one:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [view addSubview:button];

  2. - (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];

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

    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.

  4. 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

  5. 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.

  6. Here you can create dynamically a UIButton:

    //For button image
    UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
    //Custom type button
    btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
    //Set frame of button means position
    btnclose.frame = CGRectMake(103, 257, 94, 32);
    //Button with 0 border so it's shape like image shape
    [btnclose.layer setBorderWidth:0];
    //Set title of button
    [btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
    [btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
    //Font size of title
    btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    //Set image of button
    [btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];