Changing CGRectMake (x,x,x,x) to a different location such as (y,y,y,y)

Jack Humphries picture Jack Humphries · Jul 8, 2011 · Viewed 31.9k times · Source

I have placed a button in my view using CGRectMake(x,x,x,x), x being the location and size of course. When I rotate the view using -(BOOL)shouldAutoRotate... I want to change the location of the button from what would be the center in portrait mode to the center in landscape mode. The button contains information in its label that the user sets, so I DON'T want to use a different view for the landscape orientation. What if they set something in portrait and rotate to horizontal? They'll lose their data. So my question is: how do I move something that was previously set? See the code below, I don't want to realloc the button. Thanks!

// DATE

lblDate = [[UILabel  alloc] initWithFrame:CGRectMake(x, y, width, height)];

lblDate.text = @"Date:";
lblDate.backgroundColor = [UIColor clearColor];
[contentView addSubview:lblDate];

Answer

George Johnston picture George Johnston · Jul 8, 2011

Just set the frame equal to a new Rect, e.g.

lblDate.frame = CGRectMake(x,y,width,height);