lakshmen
lakshmen

Reputation: 29094

having one image on top of another for buttons in objective-c

I would like to display one image on top of another image. Basically i am trying to show the box is checked. The problem is that when click the unchecked image, the check button appears but the checkbox goes off.. I would like the check symbol to appear on top of the checkbox.

The code looks like this:

if (ShopCartUnedited) {
            [button setBackgroundImage:[UIImage imageNamed:@"none.png"] forState:UIControlStateNormal];
            [arrOfObjects addObject:@"NO"];
        }
        else {
            if (counter+1 <= nQty) {
                [button setBackgroundImage:[UIImage imageNamed:@"check2.png"] forState:UIControlStateNormal];
                [arrOfObjects addObject:@"YES"];
            }
            else {
                [button setBackgroundImage:[UIImage imageNamed:@"none.png"] forState:UIControlStateNormal];
                [arrOfObjects addObject:@"NO"];
            } 
        }

How do i change such the check2.png is on top of none.png when if it clicked...

Upvotes: 1

Views: 781

Answers (2)

Hindu
Hindu

Reputation: 2924

the simple one is use box image as background image of button in both selected and default state while set check image as image on button for selected state. And manage condition for make button selected or unselected.

like below: btn.selected = !btn.selected;

thanks

Upvotes: 0

iDev
iDev

Reputation: 23278

Either you can have two different images, one with check mark and other without checkmark and switch between them. If that is not possible, you need to add a separate imageview on top of button for check2.png

//create an imageview and add on button, adjust the frame as required.
[button addSubview:imageview];
imageview.userInteractionEnabled = NO;//if button is not accepting the touches
[button setBackgroundImage:[UIImage imageNamed:@"none.png"] forState:UIControlStateNormal];

        if (ShopCartUnedited) {
            [imageview setImage:nil];
            [arrOfObjects addObject:@"NO"];
        }
        else {
            if (counter+1 <= nQty) {
                [imageview setImage:[UIImage imageNamed:@"check2.png"]];
                [arrOfObjects addObject:@"YES"];
            }
            else {
                [imageview setImage:nil];
                [arrOfObjects addObject:@"NO"];
            } 
        }

or instead of this if-else block, just use

        if ((ShopCartUnedited) || (counter+1 > nQty)) {
            [imageview setImage:nil];
            [arrOfObjects addObject:@"NO"];
        } else {
             [imageview setImage:[UIImage imageNamed:@"check2.png"]];
             [arrOfObjects addObject:@"YES"];              
        }

Upvotes: 1

Related Questions