ios 将 CGRectMake (x,x,x,x) 更改为不同的位置,例如 (y,y,y,y)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6628644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing CGRectMake (x,x,x,x) to a different location such as (y,y,y,y)
提问by Hyman Humphries
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!
我使用 CGRectMake(x,x,x,x) 在我的视图中放置了一个按钮,当然 x 是位置和大小。当我使用 -(BOOL)shouldAutoRotate 旋转视图时...我想将按钮的位置从纵向模式的中心更改为横向模式的中心。该按钮在其标签中包含用户设置的信息,因此我不想为横向使用不同的视图。如果他们将某些东西纵向设置并旋转到横向怎么办?他们会丢失数据。所以我的问题是:我如何移动以前设置的东西?看下面的代码,我不想重新分配按钮。谢谢!
// DATE
lblDate = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
lblDate.text = @"Date:";
lblDate.backgroundColor = [UIColor clearColor];
[contentView addSubview:lblDate];
回答by George Johnston
Just set the frame equal to a new Rect, e.g.
只需将框架设置为一个新的 Rect,例如
lblDate.frame = CGRectMake(x,y,width,height);
回答by Francisco Ryan Tolmasky I
lblDate.frame = newRect
lblDate.frame = newRect
But you should probably be using autoresizing flags for this.
但是您可能应该为此使用自动调整大小标志。
回答by Perception
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.
框架
框架矩形,它描述了视图在其父视图坐标系中的位置和大小。
@property(nonatomic) CGRect 框架讨论这个矩形定义了视图在其父视图坐标系中的大小和位置。您可以在布局操作期间使用此矩形来调整视图的大小和位置。设置此属性会相应地更改由 center 属性指定的点和边界矩形中的大小。框架矩形的坐标始终以点为单位指定。
警告:如果转换属性不是身份转换,则此属性的值未定义,因此应忽略。
更改框架矩形会自动重新显示接收器,而无需调用 drawRect: 方法。如果您希望在框架矩形更改时调用 drawRect: 方法,请将 contentMode 属性设置为 UIViewContentModeRedraw。
可以动画化对此属性的更改。但是,如果转换属性包含非身份转换,则框架属性的值未定义且不应修改。在这种情况下,您可以使用 center 属性重新定位视图并使用 bounds 属性调整大小。