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

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];


Tuesday, December 6, 2011

UIButton init


     gameOverBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     gameOverBtn.frame = CGRectMake(0, 0, 150, 40);
     gameOverBtn.center = CGPointMake(160, 230);
     gameOverBtn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
     gameOverBtn.backgroundColor = [UIColor clearColor];
     [gameOverBtn setTitle:@"Game Over" forState:UIControlStateNormal];
     [gameOverBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
     [gameOverBtn setTitle:@"Restart" forState:UIControlStateHighlighted];
     [gameOverBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
     [gameOverBtn addTarget:self action:@selector(restartBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
     
     UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
     UIImage *stretchableButtonImageNormal = [buttonImageNormal
     stretchableImageWithLeftCapWidth:12 topCapHeight:0];
     [gameOverBtn setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
     
     UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
     UIImage *stretchableButtonImagePressed = [buttonImagePressed
     stretchableImageWithLeftCapWidth:12 topCapHeight:0];
     [gameOverBtn setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
     
     [self.view addSubview:gameOverBtn];