iPhone SDK UIButton

March 23, 2009 § 2 Comments

UILabel
Allow you to setup max. no. of line

UIButton
Button’s size varies with length of title. Button can’t support multiple lines and linebreak. Using background image or frame won’t support multiple lines. It’s possible to addSubview a UILabel to the button.

UIButton *button;

// Set UIbutton type
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// Set UIbutton background image for Normal state
[button setBackgroundImage:img forState:UIControlStateNormal];

// Set UIButton background image for Highlighted state
[button setBackgroundImage:buttonBackgroundPressed forStates:UIControlStateHighlighted];

// Set UIButton title for Normal, Highlighted, Selected States
[button setTitle:@”Normal State” forState:UIControlStateNormal];
[button setTitle:@”Highlighted State” forState:UIControlStateHighlighted];
[button setTitle:@”Selected State” forState:UIControlStateSelected];

// Set UIButton font
button.font = [UIFont boldSystemFontOfSize:BUTTON_FONT_SIZE];

// Set UIButton horizontal and vertical alignment
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

// Assign method to UIButton action and control event
[button addTarget:self action:@selector(buttonMethod) forControlEvents:UIControlEventTouchUpInside];

// set UIButton to center
button.center = CGPointMake( 45.0, 240.0);

// set UIButton background color
button.backgroundColor = [UIColor clearColor];

// addSubview to UIButton
[ContentView addSubview:oneButton];

// release UIButton memory
[button release];

Advertisements

§ 2 Responses to iPhone SDK UIButton

  • smita says:

    I used the following code to add button to image i.e i want to click on the image,b’t it displaying only the image,it is not clickble.
    Kindly reply me soon

    UIImage *image = [UIImage imageNamed: @”global.jpg”];

    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];

    imageview.frame=CGRectMake(25, 210, 120, 120);
    self.view=imageview;
    [imageview release];
    //UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10,10,200,40)];
    UIButton *button;

    // Set UIbutton type
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //UIImage *image = [UIImage imageNamed: @”global.jpg”];

    // Set UIbutton background image for Normal state
    [button setBackgroundImage:image forState:UIControlStateNormal];

    // Set UIButton background image for Highlighted state
    //[button setBackgroundImage:img forState:UIControlStateHighlighted];

    // Set UIButton title for Normal, Highlighted, Selected States
    [button setTitle:@”Normal State” forState:UIControlStateNormal];
    [button setTitle:@”Highlighted State” forState:UIControlStateHighlighted];
    [button setTitle:@”Selected State” forState:UIControlStateSelected];

    // Set UIButton font
    //button.font = [UIFont boldSystemFontOfSize:BUTTON_FONT_SIZE];

    // Set UIButton horizontal and vertical alignment
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    // Assign method to UIButton action and control event
    [button addTarget:self action:@selector(buttonMethod) forControlEvents:UIControlEventTouchUpInside];

    // set UIButton to center
    button.center = CGPointMake( 45.0, 240.0);

    // set UIButton background color
    button.backgroundColor = [UIColor clearColor];

    • svwebbuilder says:

      Sorry I am showing snippet of codes to demonstrate how to style UIButton. I didn’t include the entire project as I didn’t intend to offer tutorial samples like other blogs. My snippet basically show you another way to create, configure and style UIButton without using Interface Builder.

      You need to create IBAction and assign IBAction method to the button. I’ll have to find spare time to explain “how-to”. In the meantime, download the iphone Developer program’s sample code “SysSound”. Open the project to review the .h and .m files. Open the .xib to review the UI Objects on Interface Builder.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading iPhone SDK UIButton at Web Builders.

meta

%d bloggers like this: