What is the logic behind UIView's frame origin

2019-06-08 11:46发布

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 ?

1条回答
【Aperson】
2楼-- · 2019-06-08 12:09

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)

查看更多
登录 后发表回答