Saturday, February 25, 2012

Add effect to the rounded button; create the customed button

If you want to see the color change on the button in the normal stage and button-pressed stage, you may add the below code in the viewDidLoad.



 //create the button to clear Screen
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    
    //set the position of the button
    button.frame = CGRectMake(0, 0, 150, 40);
    button.center = CGPointMake(160, 240);
    
    //set the button's title
    [button setTitle:@"Clear!" forState:UIControlStateNormal];
    
    //set button effect
    UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [button setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
    
    UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchableButtonImagePressed = [buttonImagePressed   stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [button setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
    
    //add the button to the view
    [self.view addSubview:button];


 whiteButton.png

 blueButton.png
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


The following code create the customed button with own image.



UIImage *buttonImage = [UIImage imageNamed:@"cancel.png"];

    UIbutton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clearScreen:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];


No comments:

Post a Comment