Reputation: 217
I am making an iPhone game where I have the user selecting what color they want their car to be in the game. To show which cars can be selected I have UIButtons with background images that are the various cars. To show which car is currently selected I have a button behind the current car color that has a background color of yellow. What I want to happen is that when you click on a car button the yellow button moves behind the button that was clicked. My code looks like:
-(void)setPlayerCar:(UIButton *)newCar{
//this code is being called when any of the buttons is clicked
NSArray *carFiles = [NSArray arrayWithObjects:@"redCar.png",@"blueCar.png",@"greenCar.png",@"purpleCar.png",@"turquioseCar.png",@"yellowCar.png", nil];
NSString *file = [carFiles objectAtIndex:newCar.tag];
currentCarImage = file;
CGRect frame;
if(currentCarImage == @"redCar.png"){
frame = CGRectMake(48, 78, 30, 30);
}
if(currentCarImage == @"blueCar.png"){
frame = CGRectMake(83, 78, 30, 30);
}
if(currentCarImage == @"greenCar.png"){
frame = CGRectMake(118, 78, 30, 30);
}
if(currentCarImage == @"purpleCar.png"){
frame = CGRectMake(153, 78, 30, 30);
}
if(currentCarImage == @"turquioseCar.png"){
frame = CGRectMake(188, 78, 30, 30);
}
if(currentCarImage == @"yellowCar.png"){
frame = CGRectMake(223, 78, 30, 30);
}
for(UIButton *button in self.pauseScroll.subviews){
if(button.backgroundColor == [UIColor yellowColor]){
button.bounds = frame;
}
if(button.tag == newCar.tag){
[self.pauseScroll bringSubviewToFront:button];
}
}
}
As far as I know this should move the yellow colored button over to the button that was selected. The problem is this doesn't happen, when I debug I find that the correct button is being recognized and that frame is being given the right value and that the line: button.bounds = frame; is being executed but when I look at what is being displayed nothing has changed.
Upvotes: 0
Views: 1339
Reputation: 30846
You should be changing the frame
of the button and not the bounds
. The bounds
of a view describe a view's location and size in its own coordinate space. The frame
is the size and location in the superview's coordinate space.
if (button.backgroundColor == [UIColor yellowColor]) {
button.frame = frame;
}
Upvotes: 1