现在的位置: 首页 > web前端 > 正文

layoutsubviews何时调用

2020年07月16日 web前端 ⁄ 共 1192字 ⁄ 字号 评论关闭

  在UIView里面有一个方法layoutSubviews,这个方法具体作用是什么呢?


  layoutsubviews官方文档解释


  我们先来看看苹果官方文档的解释:


  ThedefaultimplementationofthismethoddoesnothingoniOS5.1andearlier.Otherwise,thedefaultimplementationusesanyconstraintsyouhavesettodeterminethesizeandpositionofanysubviews.


  Subclassescanoverridethismethodasneededtoperformmorepreciselayoutoftheirsubviews.Youshouldoverridethismethodonlyiftheautoresizingandconstraint-basedbehaviorsofthesubviewsdonotofferthebehavioryouwant.Youcanuseyourimplementationtosettheframerectanglesofyoursubviewsdirectly.


  Youshouldnotcallthismethoddirectly.Ifyouwanttoforcealayoutupdate,callthesetNeedsLayoutmethodinsteadtodosopriortothenextdrawingupdate.Ifyouwanttoupdatethelayoutofyourviewsimmediately,callthelayoutIfNeededmethod.


  最后一段说,不要直接调用此方法。如果你想强制更新布局,你可以调用setNeedsLayout方法;如果你想立即数显你的views,你需要调用layoutIfNeeded方法。


  layoutSubviews作用


  layoutSubviews是对subviews重新布局。比如,我们想更新子视图的位置的时候,可以通过调用layoutSubviews方法,既可以实现对子视图重新布局。


  layoutSubviews默认是不做任何事情的,用到的时候,需要在自雷进行重写。


  layoutSubviews以下情况会被调用


  苹果官方文档已经强调,不能直接调用layoutSubviews对子视图进行重新布局。那么,layoutSubviews什么情况下会被调用呢?通过百度搜索,发现以下几种情况layoutSubviews会被调用。


  直接调用setLayoutSubviews。(这个在上面苹果官方文档里有说明)


  a总之,layoutsubviews给大家简单的介绍了一些,希望大家多看看。


  

抱歉!评论已关闭.