更改 ios Frame Size(width e height) 保持位置 (x,y)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4349551/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:13:34  来源:igfitidea点击:

Change ios Frame Size(width e height) keeping position (x,y)

cocoa-touchios

提问by MatterGoal

How can i change Size of a Frame view and keep origin position? I tried with this code (but it didn't work):

如何更改框架视图的大小并保持原点位置?我尝试使用此代码(但没有用):

myview.frame.size = CGSizeMake(23.0,50.0); 

回答by runmad

Here's how to do it with just one line of code:

以下是仅使用一行代码的方法:

myview.frame = CGRectMake(23, 50, myview.frame.size.width, myview.frame.size.height);

or

或者

[myview setFrame:CGRectMake(23, 50, myview.frame.size.width, myview.frame.size.height)];

回答by Jeremy Fuller

You need to set the whole frame at once. Try:

您需要一次设置整个框架。尝试:

CGRect newFrame = myview.frame;
newFrame.size = CGSizeMake(23.0, 50.0);
myview.frame = newFrame;

回答by pierre23

Not that I am aware of. Rather than change your view's frame property, you can change the bounds property so regardless of what you set for x and y, it wont affect your view position. Make sure that you use 0 for x and y.

不是我所知道的。您可以更改 bounds 属性,而不是更改视图的 frame 属性,因此无论您为 x 和 y 设置什么,它都不会影响您的视图位置。确保对 x 和 y 使用 0。

self.myView.bounds = CGRectMake(0,0, newWidth, newHeight);