为什么我们不能直接改变UIView框架的大小或来源? [英] Why can't we directly change size or origin of a UIView frame?

查看:76
本文介绍了为什么我们不能直接改变UIView框架的大小或来源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些知道这一点的人来说听起来可能很愚蠢,但我对iOS很新。
我想知道,为什么我们不能修改原点或大小,或者可能是UIView框架的这些属性的x,y或高度宽度?

It may sound very silly to people who know this but I am quite new to iOS. I wanted to know, why cant we modify origin or size or may be x,y or height width of these properties of a UIView frame ?

框架是CGRect结构,原点和大小也是CGPoint和CGSize结构。
当我们可以访问它们时,为什么我们无法修改它们?

frame is a CGRect struct and origin and size are again CGPoint and CGSize structures. when we can access them, why cant we modify them ?

在CGRect中没有这样的东西,我们可以修改它的属性,所以为什么我们不能这样做它用于框架或边界。

There is nothing like this in a CGRect, we can modify it's attributes so why cant we do it for frame or bounds.

推荐答案

我认为你的意思是为什么你不能做这样的事情:

I assume you mean why can't you do somethIng like this:

myView.frame.origin.x = 23.0;

这是一个很好的问题,对此毫无愚蠢。问题是框​​架被保持为视图的属性,而不是框架的各个组件(尺寸和原点,甚至更深,宽度,长度,x和y)。

It's a good question, nothing silly about it. The problem is that the frame is held as the property of the view, not the individual components of the frame (the size and origin, or even deeper, the width, length, x and y).

框架是视图的基础,因此如果视图的框架被修改,视图需要执行许多操作。如果您要直接进入并修改origin.x,那么您将绕过 setFrame 方法,其中可能发生所有这种魔法。

The frame is fundamental to the view, so there are lots of actions that a view needs to do if its frame is modified. If you were to reach in and modify the origin.x directly then you would be bypassing the setFrame method where all of this magic probably happens.

当您访问view.frame时,您将获得一个与view.frame,具有相同值的CGRect,但实际上并不是框架 ,因此对它的任何修改都不会影响视图。

When you access view.frame you are being given a CGRect that has the same value as view.frame, but is not actually the frame, so any modifications to it don't affect the view.

你可以分步进行:

CGRect frame = view.frame;
frame.origin.x = 23.0;
view.frame = frame;

这篇关于为什么我们不能直接改变UIView框架的大小或来源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆