iOS create UIButton programmatically

Button is an important element of any screen design. A button can intercept touch events and send action message to a target object when tapped. In this example we are going to create a standard button as well as an image button. We are going to learn the following
  • Create a button with standard style
  • Place the button at a given position on the screen
  • Set the size of the button
  • Create custom button using images as button background
  • Set button title and image background based on current state
  • Listen for button touch events such as UIControlEventTouchDown and UIControlEventTouchUpInside and take necessary action
Click here Create button programmatically in Swift using Auto Layout Visual Format Language (VFL)
iOS create UIButton programmatically
iOS standand Button pressed mode
iOS create Image Button programmatically

Interface file for the view controller - ButtonExampleViewController.h

#import <UIKit/UIKit.h>

@interface ButtonExampleViewController : UIViewController

@property (nonatomic, strong) UIButton *myButton, *myImageButton;

@end

Implementation file for the view controller - ButtonExampleViewController.m

#import "ButtonExampleViewController.h"

@interface ButtonExampleViewController ()

@end

@implementation ButtonExampleViewController

@synthesize myButton, myImageButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    
    //set the background to white color
    self.view.backgroundColor = [UIColor whiteColor];
    
    //create a rounded rectangle type button
    self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //set the button size and position
    self.myButton.frame = CGRectMake(85.0f, 100.0f, 150.0f, 37.0f);
    
    //set the button title for the normal state
    [self.myButton setTitle:@"Press This Button"
                   forState:UIControlStateNormal];
    //set the button title for when the finger is pressing it down
    [self.myButton setTitle:@"Button is Pressed"
                   forState:UIControlStateHighlighted];
    //add action to capture the button press down event
    [self.myButton addTarget:self
                      action:@selector(buttonIsPressed:)
            forControlEvents:UIControlEventTouchDown];
    //add action to capture when the button is released
    [self.myButton addTarget:self
                      action:@selector(buttonIsReleased:)
            forControlEvents:UIControlEventTouchUpInside];
    //set button tag
    [self.myButton setTag:1];
    //add the button to the view
    [self.view addSubview:self.myButton];
    
    
    //create a custom button using background as Image
    self.myImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //create background images and dump them in the supporting files folder
    //create an UIImage object from the background images
    UIImage *normalState = [UIImage imageNamed:@"normal-button.png"];
    UIImage *pressedState = [UIImage imageNamed:@"pressed-button.png"];
    
    //set the button size and position
    self.myImageButton.frame = CGRectMake(60.0f, 200.0f, 200.0f, 37.0f);
    //set the image for the button in normal state
    [self.myImageButton setBackgroundImage:normalState
                                  forState:UIControlStateNormal];
    //set the button title for the normal state
    [self.myImageButton setTitle:@"Button in Normal State"
                        forState:UIControlStateNormal];
    //set the image for the button in pressed down state
    [self.myImageButton setBackgroundImage:pressedState
                                  forState:UIControlStateHighlighted];
    //set the button title for when the finger is pressing it down
    [self.myImageButton setTitle:@"Button in Pressed State"
                        forState:UIControlStateHighlighted];
    //add action to capture the button press down event
    [self.myImageButton addTarget:self
                           action:@selector(buttonIsPressed:)
                 forControlEvents:UIControlEventTouchDown];
    //add action to capture when the button is released
    [self.myImageButton addTarget:self
                           action:@selector(buttonIsReleased:)
                 forControlEvents:UIControlEventTouchUpInside];
    //set button tag
    [self.myImageButton setTag:2];
    //add the button to the view
    [self.view addSubview:self.myImageButton];
}

- (void) buttonIsPressed:(UIButton *)paramSender{
    switch (paramSender.tag) {
        case 1:
            NSLog(@"My Standard Button is pressed down.");
            break;
        case 2:
            NSLog(@"My Custom Image Button is pressed down.");
            break;
        default:
            NSLog(@"No idea which Button is pressed down.");
            break;
    }
}

- (void) buttonIsReleased:(UIButton *)paramSender{
    switch (paramSender.tag) {
        case 1:
            NSLog(@"My Standard Button was just released.");
            break;
        case 2:
            NSLog(@"My Custom Image Button was just released.");
            break;
        default:
            NSLog(@"No idea which Button was just released.");
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Reference

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.