xcode 是否可以在不设置原点的情况下以编程方式设置 UIView 的高度和宽度?

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

Is it possible to set the height and width of a UIView programmatically without setting the origin?

iosobjective-cxcodeconstraints

提问by Joel Derfner

I've just started working with constraints, and I'm finding it onerous to have to set both the location and the size of a UIView using constraints--four constraints seems excessive. (I don't doubt that it's possible to do with fewer constraints; I'm just still working with a very basic understanding!) Is it possible to set the size of a UIView, therefore, not using constraints, but then set the location using constraints? The only non-constraint ways I know to set size also involve setting origin.

我刚刚开始使用约束,我发现必须使用约束来设置 UIView 的位置和大小很麻烦——四个约束似乎过多。(我不怀疑可以用更少的约束来做;我只是在一个非常基本的理解下工作!)是否可以设置 UIView 的大小,因此,不使用约束,然后设置位置使用约束?我知道设置大小的唯一非约束方法也涉及设置原点。

回答by Joris Kluivers

If you are using AutoLayout to manage view position and size it's not advised to adjust a view's frame. Most of the time this won't work anyway.

如果您使用 AutoLayout 来管理视图位置和大小,则不建议调整视图的框架。大多数时候这无论如何都行不通。

But even AutoLayout needs to know about size and position. So you'll always end up having to specify both. If like you ask it would be possible to set less than 4 constraints (by not specifying it's position for example), AutoLayout would not know where to show the view.

但即使是 AutoLayout 也需要知道大小和位置。所以你最终总是不得不同时指定两者。如果像您问的那样,可以设置少于 4 个约束(例如,通过不指定它的位置),AutoLayout 将不知道在哪里显示视图。

What is it you find hard about having to specify 4 constraints? This is done automatically for you in your XIB/Storyboard file. If you are writing everything in code checkout the convenience ASCII-art-format stringswhich lets you specify position and sizing using a 'visual' string representation.

您觉得必须指定 4 个约束有什么困难?这是在您的 XIB/故事板文件中为您自动完成的。如果您在代码中编写所有内容,请查看方便的ASCII 艺术格式字符串,它允许您使用“视觉”字符串表示指定位置和大小。

回答by ryanwils

It seems like you're just trying to set the frame of the view (both the origin, and size).

看起来您只是想设置视图的框架(原点和大小)。

You can use something like this to do so:

你可以使用这样的东西来做到这一点:

CGRect newFrame = CGRectMake( xCoordinate, yCoordinate, width, height);
myView.frame = newFrame;

Where xCoordinate, yCoordinate, width and height are whatever you'd like.

其中 xCoordinate、yCoordinate、width 和 height 是您想要的。

Hope that helps! Let me know if it's not the answer you were looking for and I'll try to help.

希望有帮助!如果这不是您要找的答案,请告诉我,我会尽力提供帮助。

回答by JDL

Supposedly:

据说:

The geometry of a view is defined by its frame, bounds, and center properties. The frame defines the origin and dimensions of the view in the coordinate system of its superview and is commonly used during layout to adjust the size or position of the view. The center property can be used to adjust the position of the view without changing its size. The bounds defines the internal dimensions of the view as it sees them and is used almost exclusively in custom drawing code. The size portion of the frame and bounds rectangles arecoupled together so that changing the size of either rectangle updates the size of both.

视图的几何形状由其框架、边界和中心属性定义。框架定义了视图在其父视图坐标系中的原点和尺寸,通常在布局时用于调整视图的大小或位置。center 属性可用于在不改变其大小的情况下调整视图的位置。边界定义了视图看到的内部尺寸,并且几乎专门用于自定义绘图代码。框架和边界矩形的大小部分耦合在一起,因此更改任一矩形的大小都会更新两者的大小。

So, theoretically, you can change the size of the bounds, where the origin is always 0,0.

因此,理论上,您可以更改边界的大小,其中原点始终为 0,0。

回答by jrturton

Setting the boundsproperty of a view will change its size without you explicitly setting an origin - from the docs:

设置bounds视图的属性将更改其大小,而无需您明确设置原点 - 来自文档:

Changing the bounds size grows or shrinks the view relative to its center point.

更改边界大小会相对于其中心点增大或缩小视图。

You can try setting an appropriate resizing mask (probably flexible margins all round?) which will be translated into constraints for you, and then use positioning constraints to move it.

您可以尝试设置一个适当的调整大小掩码(可能是灵活的边距?),它将为您转换为约束,然后使用定位约束来移动它。

However, I'd personally recommend going all in with constraints rather than mixing and matching. The visual format would allow you to specify size and position pretty simply.

但是,我个人建议全部采用约束而不是混合和匹配。视觉格式将允许您非常简单地指定大小和位置。