Reputation: 13267
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];
Upvotes: 5
Views: 31954
Reputation: 80603
As found in the UIView class reference.
frame
The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
@property(nonatomic) CGRect frame Discussion This rectangle defines the size and position of the view in its superview’s coordinate system. You use this rectangle during layout operations to size and position the view. Setting this property changes the point specified by the center property and the size in the bounds rectangle accordingly. The coordinates of the frame rectangle are always specified in points.
Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
Changing the frame rectangle automatically redisplays the receiver without invoking the drawRect: method. If you want the drawRect: method invoked when the frame rectangle changes, set the contentMode property to UIViewContentModeRedraw.
Changes to this property can be animated. However, if the transform property contains a non-identity transform, the value of the frame property is undefined and should not be modified. In that case, you can reposition the view using the center property and adjust the size using the bounds property instead.
Upvotes: 0
Reputation: 6173
lblDate.frame = newRect
But you should probably be using autoresizing flags for this.
Upvotes: 0
Reputation: 32258
Just set the frame equal to a new Rect, e.g.
lblDate.frame = CGRectMake(x,y,width,height);
Upvotes: 14