在viewDidLayoutSubviews中的框架计算 [英] Frame calculations in `viewDidLayoutSubviews`

查看:72
本文介绍了在viewDidLayoutSubviews中的框架计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我应该提到,这主要是效率问题.

First, I should mention, this is mostly an efficiency issue.

关于viewWillAppear太早而viewDidAppear太晚(视图已可见)的帧计算,有很多讨论.

There are many discussions as to where to do frame calculations where viewWillAppear is too early and viewDidAppear is too late (view is already visible).

常见的答案是在viewDidLayoutSubviews中进行帧计算.问题是,它被多次调用.更糟糕的是,最准确的呼叫是所有帧都具有最终大小的呼叫,是最后一个呼叫.据我所知,没有办法知道哪个叫最后一个.

The common answer is to do frame calculations in viewDidLayoutSubviews. Problem is, it gets called multiple times. Worse, the most accurate call, the one where all frames have their final size is the last one. To my knowledge there is no way to know which call is that final one.

我们曾经有一个'framesAreSet'标志(初始化为false)和一个检查,如果frame不为零(例如self.view.frame.size.width != 0之类)并且'framesAreSet'为false,它将进入,转动标志并仅计算一次.像这样:

We use to have a 'framesAreSet' flag (initialized false) and a check, where if frame is not zero (something like self.view.frame.size.width != 0) and 'framesAreSet' is false, it goes in, turn the flag and calculate only once. Something like:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    if (self.view.frame.size.width != 0 && !framesAreSet)
    {
        framesAreSet = true;

        //Calculate frames here
    }
} 

这看起来还可以,但实际上,对self.view.frame.size.width != 0之类的内容进行检查并不能保证确实设置了帧. viewDidLayoutSubviews将被调用的事实表明某些帧未设置为最终状态.

This looks ok, but in truth, a check for something like self.view.frame.size.width != 0 does not guarantee that frames are indeed set. The fact that viewDidLayoutSubviews will get called after suggests that some frames were not set to their final state.

拥有一个viewCompleteLayoutSubviews非常好.在设置了所有帧并且还看不到视图时,有什么想法是完成一次性"帧计算的最佳方法?

Would be great to have a viewCompleteLayoutSubviews. Any ideas of what's the best way to accomplish a 'one time' frame calculations when all frames are set and view is not yet visible?

(这可能是使用NSConstraints的视图的问题)

(This is probably an issue with views not using NSConstraints)

推荐答案

该应用每次平局仅获得一个didLayoutSubviews(至此,视图状态更改仅用setNeedsLayout记录).从这个意义上讲,didLayoutSubviews 是您对didCompleteLayoutOfSubviews的想法.绘制后,如果发生另一个视图状态更改,则布局将再次不完整.

The app gets only one didLayoutSubviews per draw (up to that point, view state changes are just noted with setNeedsLayout). In that sense, didLayoutSubviews is your idea of didCompleteLayoutOfSubviews. After the draw, if another view state change happens, the layout is incomplete again.

换句话说,didLayout调用的数量不取决于子视图添加或框架更改的数量,而是取决于绘制的数量(不要与运行循环混淆).在绘制之前,如果已经设置了needsLayout标志,则无论重新布置了多少视图层次结构,都将只调用一次layoutSubviews和didLayoutSubviews.

In other words, number of didLayout calls doesn't depend on the number of subview adds or frame changes, it depends on the number of draws (not to be confused with the run loop). Before a draw, if the needsLayout flag has been set, layoutSubviews and then didLayoutSubviews will be called exactly once, no matter how much the view hierarchy has been rearranged.

这篇关于在viewDidLayoutSubviews中的框架计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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