Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Change background color of uibutton using subclass method

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 525
    Comment on it

    Hello all !
    Explanation is,
    Suppose we want to change the background color of the UIButton when it is selected and when reSelected change it to the default colour.

    There are many methods for doing so and one of them I am explaining below :-

    • First Make a subClass of UIButton and assign that Class to buttons on which we want to perform the action and then do the following :-

    Implement this code in the subClass of button :-

    - (void) setSelected:(BOOL)selected {
        
        if (selected) {
            self.backgroundColor =[UIColor blueColor]];  // set the background color
        }
        else {
            self.backgroundColor = _originalColor;  
        }
        [super setSelected:selected];
    
    }
    
    -(void)awakeFromNib{
        
        _originalColor = self.backgroundColor;  // original color is the property used for assigning the default color of UIButton
    }

     

    • Now implement this code where the action of UIButton is to be implemented.

        We have to toggle in the action of UIButton like this:-

    - (IBAction)btnChangeBGcolor:(UIButton *)sender {
      
         // perform the actions of buttons
         sender.selected = !sender.selected;
        _btnChat.selected=NO;        // other buttons responses when one UIButton is selected 
        _btnProfile.selected=NO;
       
     // do your code . . .
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: