UIView 的框架原点和大小为只读背后的逻辑是什么 [英] What is the logic behind UIView's frame origin and size being read-only

查看:64
本文介绍了UIView 的框架原点和大小为只读背后的逻辑是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果你想调整 UIView 的大小,你必须这样做:

CGRect frame = view.frame;frame.size.height -= 100;view.frame = 框架;

而不仅仅是:

view.frame.size.height -= 100;

我的问题是这背后的逻辑是什么?frame 的 setter 是否做了一些额外的事情,比如调用 setNeedsDisplay ?

解决方案

基本上是下面这行的原因

view.frame.size.height -= 100;

实际上做了以下两件事:

  1. 调用返回 CGRect 结构的 [view frame] 方法 - copy 视图中存储的内容
  2. 然后它改变了副本的height字段——所以它不会影响存储在视图中的结构的值

<块引用>

框架的设置器是否做了一些额外的事情,比如调用设置需要显示?

实际上是的 - 视图的 frame 属性是根据它的原点、边界和变换计算的,所以设置框架也会影响它的原点和边界(并且取决于视图的 contentMode 可能会强制视图重绘)

I know that if you want to resize a UIView you have to do it like so:

CGRect frame = view.frame;
frame.size.height -= 100;
view.frame = frame;

instead of just:

view.frame.size.height -= 100;

My question is what could be the logic behind this? Is the setter for frame doing something extra, like maybe call setNeedsDisplay ?

解决方案

Basically the reason behind it that the following line

view.frame.size.height -= 100;

actually does the following 2 things:

  1. Calls [view frame] method that returns CGRect structure - copy of what is stored in your view
  2. Then it changes the height field of the copy - so it does not affect the value of the structure stored in the view

Is the setter for frame doing something extra, like maybe call setNeedsDisplay ?

Actually yes - view's frame property is calculated based on its origin, bounds and transform so setting the frame will also affect its origin and bounds (and depending on view's contentMode may force the view to redraw)

这篇关于UIView 的框架原点和大小为只读背后的逻辑是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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