UIView的setNeedsLayout,layoutIfNeeded和layoutSubviews之间有什么关系? [英] What is the relationship between UIView's setNeedsLayout, layoutIfNeeded and layoutSubviews?

查看:150
本文介绍了UIView的setNeedsLayout,layoutIfNeeded和layoutSubviews之间有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以对 UIView的 setNeedsLayout layoutIfNeeded <之间的关系给出明确的解释/ code>和 layoutSubviews 方法?以及将使用所有三个的示例实现。谢谢。

Can anyone give a definitive explanation on the relationship between UIView's setNeedsLayout, layoutIfNeeded and layoutSubviews methods? And an example implementation where all three would be used. Thanks.

令我困惑的是,如果我向我的自定义视图发送 setNeedsLayout 消息,那么下一件事就是它在此方法 layoutSubviews 之后调用,跳过 layoutIfNeeded 。从文档中我预计流量为 setNeedsLayout >导致 layoutIfNeeded 被调用>原因 layoutSubviews 将被调用。

What gets me confused is that if I send my custom view a setNeedsLayout message the very next thing it invokes after this method is layoutSubviews, skipping right over layoutIfNeeded. From the docs I would expect the flow to be setNeedsLayout > causes layoutIfNeeded to be called > causes layoutSubviews to be called.

推荐答案

我仍在努力解决这个问题,所以如果它包含错误,请接受一些怀疑并原谅我。

I'm still trying to figure this out myself, so take this with some skepticism and forgive me if it contains errors.

setNeedsLayout 是一个简单的:它只是设置UIView中的某个标志,标志着它需要布局。这将强制在下次重绘之前在视图上调用 layoutSubviews 。请注意,在许多情况下,由于 autoresizesSubviews 属性,您无需显式调用此方法。如果设置了(默认情况下是这样),那么对视图框架的任何更改都会导致视图布局其子视图。

setNeedsLayout is an easy one: it just sets a flag somewhere in the UIView that marks it as needing layout. That will force layoutSubviews to be called on the view before the next redraw happens. Note that in many cases you don't need to call this explicitly, because of the autoresizesSubviews property. If that's set (which it is by default) then any change to a view's frame will cause the view to lay out its subviews.

layoutSubviews 是你做所有有趣事情的方法。如果愿意,它相当于 drawRect 的布局。一个简单的例子可能是:

layoutSubviews is the method in which you do all the interesting stuff. It's the equivalent of drawRect for layout, if you will. A trivial example might be:

-(void)layoutSubviews {
    // Child's frame is always equal to our bounds inset by 8px
    self.subview1.frame = CGRectInset(self.bounds, 8.0, 8.0);
    // It seems likely that this is incorrect:
    // [self.subview1 layoutSubviews];
    // ... and this is correct:
    [self.subview1 setNeedsLayout];
    // but I don't claim to know definitively.
}

AFAIK layoutIfNeeded isn通常意味着要在子类中重写。当您希望立即布置视图时,您可以调用此方法。 Apple的实现可能如下所示:

AFAIK layoutIfNeeded isn't generally meant to be overridden in your subclass. It's a method that you're meant to call when you want a view to be laid out right now. Apple's implementation might look something like this:

-(void)layoutIfNeeded {
    if (self._needsLayout) {
        UIView *sv = self.superview;
        if (sv._needsLayout) {
            [sv layoutIfNeeded];
        } else {
            [self layoutSubviews];
        }
    }
}

你会打电话给 layoutIfNeeded 在视图上强制它(及其必要时的超级视图)立即布局。

You would call layoutIfNeeded on a view to force it (and its superviews as necessary) to be laid out immediately.

这篇关于UIView的setNeedsLayout,layoutIfNeeded和layoutSubviews之间有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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