Showing posts with label UIButton buttonWithType example. Show all posts
Showing posts with label UIButton buttonWithType example. Show all posts

Thursday, May 30, 2013

UIButton buttonWithType example in Objective C (iOS).

UIButton buttonWithType

Creates and returns a new button of the specified type.

+ (id)buttonWithType:(UIButtonType)buttonType

Parameters
buttonType
The button type. See UIButtonType for the possible values.

Return Value of [UIButton buttonWithType]
A newly created button.

Discussion of [UIButton buttonWithType]
This method is a convenience constructor for creating button objects with specific configurations. It you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.

When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.

UIButton buttonWithType example.
UIButton *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];

Example of [UIButton buttonWithType].
//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];

UIButton buttonWithType example.
- (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]; 
}

End of UIButton buttonWithType example article.

Wednesday, April 17, 2013

UIButton buttonWithType example objc


buttonWithType:
Creates and returns a new button of the specified type.

+ (id)buttonWithType:(UIButtonType)buttonType

Parameters

buttonType
The button type. See UIButtonType for the possible values.

Return Value
A newly created button.


Example>
{
    NSString            *title = @"My button title";
    UIColor              *titleColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
    UIImage             *bgImage = [UIImage imageNamed:@"yourImage.png"];

    UIImage             *highlightedBgImage = [UIImage imageNamed:@"yourImage.png"];
    UIButton            *button = [UIButton buttonWithType:UIButtonTypeCustom];

    
    // Create button.
    //
    button.bounds = CGRectMake( 0.0f, 0.0f, bgImage.size.width, bgImage.size.height );
    button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0f];
    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    button.titleLabel.minimumFontSize = 10.0f;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setBackgroundImage:bgImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightedBgImage forState:UIControlStateHighlighted];

    // You must specify your target object and action selector here.
    // 
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

}