开发者

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

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 someth开发者_StackOverflow中文版ing 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];


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

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


lblDate.frame = newRect

But you should probably be using autoresizing flags for this.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜