Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set UIImage from nomal state and title text in the selected state in UIButton

How I can set UIImage in the normal state and set title in the selected state in my UIButton?

Hi, I try to set a UIImage in the normal state of UIButton, but when set an UImage in normal state this change the title of UIButton in the selected state changing the title for the image . I am using

myButton setTitle:content forState:UIControlStateSelected

in the state selected.

How I can set UIImage in the normal state and set title in the selected state in my UIButton?

PDT: sorry for my english!.

like image 659
kakashy Avatar asked Jan 29 '26 19:01

kakashy


2 Answers

Try This:

BOOL selected;

-(IBAction)btnClicked:(UIButton *)sender{
    if(selected)
    {
        [chkBtn setTitle:@"selected" forState:UIControlStateNormal];
        chkImage.image=[UIImage imageNamed:@" "]; 
        NSLog(@"%@",chkBtn.titleLabel);
        selected=NO;
    }
    else
    {
        [chkBtn setTitle:@" " forState:UIControlStateNormal];
        chkImage.image=[UIImage imageNamed:@"button_minus_pink.png"]; 
        selected=YES;
     }

}

like image 161
Dipendra Avatar answered Feb 01 '26 14:02

Dipendra


// set with UIColor 

[self.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

// set with UIImage

[self.button setBackgroundImage:[UIImage imageNamed:@"BlackRightBtn.png"] forState:UIControlStateNormal];

For Button Selected

if (self.button.isSelected == YES)
{
    [self.button setTitle:@"Your_ButtonTitle" forState:UIControlStateNormal];
}
like image 41
iPatel Avatar answered Feb 01 '26 12:02

iPatel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!