一般情况都是说UIViewController的生命周期,UIView的生命周期经常被忽视。
显示过程:
//(superview)
- (void)willmovetosuperview:(nullable UIView *)newSuperview
- (void)didmovetosuperview
//(window)
- (void)willmovetowindow:(nullable UIWindow *)newWindow
- (void)didmovetowindow
- (void)layoutsubviews
移出过程:
//(window)
- (void)willmovetowindow:(nullable UIWindow *)newWindow
- (void)didmovetowindow
//(superview)
- (void)willmovetosuperview:(nullable UIView *)newSuperview
- (void)didmovetosuperview
- (void)removeFromSuperview
- (void)dealloc
但是在移出时newWindow和newSuperview 都是nil。
当增加一个子控件时,就会执行 didaddsuperrview,之后也会执行一次layoutsubview。 在view释放后,执行完,dealloc就会多次执行willremovsubview.先add的view,先释放掉。
在上面的方法中,经常发现layoutsubview会被调用,下面说下layoutsubview的调用情况: 1、addSubview会触发layoutSubviews,如果addSubview 如果连续2个 只会执行一次,具体原因下面说。 2、设置view的Frame会触发layoutSubviews,必须是frame的值设置前后发生了变化。 3、滚动一个UIScrollView会触发layoutSubviews。 4、旋转Screen会触发父UIView上的layoutSubviews事件。 5、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。
***TIP
其中的原理是:执行setNeedsLayout后会在receiver标上一个需要被重新布局的标记,在系统runloop的下一个周期自动调用layoutSubviews。 这样刷新会产生延迟,所以我们需要马上执行layoutIfNeeded。就会开始遍历subviews的链,判断该receiver是否需要layout。如果需要立即执行layoutsubview
每一个视图只能有唯一的一个父视图。如果当前操作视图已经有另外的一个父视图,则addsubview的操作会把它先从上一个父视图中移除(包括响应者链),再加到新的父视图上面。
并且连续2次的addSubview,只会执行一次layoutsubview。因为一次的runLoop结束后,如果有需要刷新,执行一次即可。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有