如何在 iOS 中调整 UIView 的大小?

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

How to resize UIView in iOS?

iosobjective-c

提问by Vincenzo Putignano

I have got a main view and a view1 into main view, how to resize view1?

我有一个主视图和一个主视图的 view1,如何调整 view1 的大小?

This code doesn't work:

此代码不起作用:

self.View1.bounds.size.height = self.view.bounds.size.height;

回答by Tony Friz

CGRect frame = self.View1.frame;
frame.size.height = self.view.bounds.size.height;
self.View1.frame = frame;

回答by cdownie

You can't modify the bounds directly. The CGRect needs to be modified outside of the view's bounds.

您不能直接修改边界。CGRect 需要在视图边界之外修改。

CGRect viewBounds = self.view1.bounds;
viewBounds.size.height = self.view.bounds.size.height;
self.view1.bounds = viewBounds;

Hope this helps!

希望这可以帮助!

回答by Erik van der Neut

I don't have enough reputation to add a simple comment to some of the other answers, so I'll help point out a couple of important points here:

我没有足够的声誉来为其他一些答案添加简单的评论,所以我将在这里帮助指出几个要点:

(1) If just changing the size, set new boundsinstead of frame

(1) 如果只是改变大小,设置 newbounds而不是frame

If you are justlooking to resize an image (but keep the image at the same location), then follow cdownie's suggestionof setting new boundsinstead of a new frame. For example, if you have an image of a pointer hand and want to make it look as if it's tapping the screen, then scaling the size of the boundsdown and back up real quick will achieve that and it will maintain its same screen location at its center point if you set the anchor point of the image at its center. If you had changed the frameinstead as Tony Friz suggested, then upon a reduction of image size, the image will pull to its top-left corner (unless you correct for that with extra math).

如果您只是想调整图像大小(但将图像保持在同一位置),请按照cdownie 的建议设置 newbounds而不是 new frame。例如,如果您有一个指针手的图像,并想让它看起来好像在轻敲屏幕,那么快速缩放bounds向下和备份的大小将实现这一目标,并且它将在其屏幕上保持相同的位置如果将图像的锚点设置在其中心,则中心点。如果您frame按照 Tony Friz 的建议更改了,那么在缩小图像尺寸时,图像将拉到其左上角(除非您使用额外的数学进行校正)。

(2) If the image created in nib or storyboard, turn off Autolayout and Autosizing

(2) 如果图像是在 nib 或 storyboard 中创建的,请关闭 Autolayout 和 Autosizing

Autolayout needs to be turned OFF over any views in which you are moving images around or resizing them. If you leave it on, the behavior will (of course) be odd and unpredictable. Also, with Autolayout off, go into the image's Size Inspector and set the proper origin (center, for example) and disable all Autosizing constraints by clicking on any solid lines in the Autosizing grid until all of them are dotted lines. This way you will have full control over image placement by altering the centerproperty and sizing by setting new bounds.

需要在您移动图像或调整图像大小的任何视图上关闭自动布局。如果您将其保持打开状态,则行为(当然)将变得奇怪且不可预测。此外,在 Autolayout 关闭的情况下,进入图像的 Size Inspector 并设置正确的原点(例如中心)并通过单击 Autosizing 网格中的任何实线来禁用所有 Autosizing 约束,直到所有这些都是虚线。通过这种方式,您可以center通过设置 new更改属性和大小来完全控制图像放置bounds

Hope this helps, Erik

希望这会有所帮助,埃里克

回答by RyanG

[self.View1 setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.x, self.view.frame.size.height, self.view.frame.size.height)];