自动布局和放大器;程序的限制:如何应对updateConstraints射击多次? [英] Autolayout & programmatic constraints: How to deal with updateConstraints firing multiple times?

查看:106
本文介绍了自动布局和放大器;程序的限制:如何应对updateConstraints射击多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编程方式创建的布局,我按照苹果的建议是:覆盖-updateConstraints,添加自定义的约束,一旦子视图已经添加到视图调用-setNeedsUpdateConstraints。我典型的设置是这样的:

When programmatically creating layouts, I follow Apple's advice: override -updateConstraints, add custom constraints, and call -setNeedsUpdateConstraints once subviews have been added to the view. My typical setup looks like this:

- (void)setupViews
{
    //Style View

    //Add gesture recognizers

    //Add Subviews


    [self setNeedsUpdateConstraints];
}

- (void)updateConstraints
{
    //Add custom constraints       

    [super updateConstraints];
}

问题

有当-updateConstraints被炒鱿鱼多次(例如,当一个视图控制器是presented或推W /动画)的场合。这里的问题是,添加的每个约束被重新添加。试图按需改变一个加约束的常数时,这成为一个严​​重的问题,因为有两个原始约束随后彼此冲突的。我想,即使看起来在不创建它们,有双后操作的约束你并不好。

There are occasions when -updateConstraints gets fired multiple times (for example, when a view's controller is presented or pushed w/ animation). The problem here is that each constraint added gets re-added. This becomes a serious problem when trying to on-demand change the constant of an added constraint, since there are two of the original constraint that subsequently conflict with each other. I imagine that even when you aren't manipulating the constraints after creating them, having double what you doesn't seem good.

潜在的解决方案

1 - 中-updateConstraints应用之前取下影响视图中的所有约束:

1 - Remove all constraints effecting the view before applying them in -updateConstraints:

 - (void)updateConstraints
    {   
        //Remove all constraints affecting view & subviews

        //Add custom constraints

        [super updateConstraints];       
    }

2 - 设置布局标志和放大器;对证添加自定义约束之前,

2 - Set a layout flag & check against it before adding custom constraints:

- (void)updateConstraints
{
    if (self.didAddConstraints) {
        [super updateConstraints];
        return;
    }

    //Add custom constraints   

    self.didAddConstraints = YES;    

    [super updateConstraints];
}

3 - 不要担心限制上加倍,每当改变一个常量是必要的,只有前重新添加删除约束

3 - Don't worry about doubling up on constraints, and whenever changing a constant is needed, only remove that constraint before re-adding.

3 - 真棒的东西,我都没有想到的。

3 - Something awesome that I haven't thought of.

什么是最好的做法吗?

What's the best practice here?

推荐答案

简短的回答:潜在的解决方案2号

Short answer: Potential solution number 2.

卸下和重新应用所有约束可能会变得昂贵,因为布局变得更加复杂。此外,如果你的布局是有状态的,你有更多的问题。

Removing and reapplying all constraints may become costly as the layout becomes more complex. Besides, if your layout is stateful, you'd have more problems.

倍增的约束是非常低效的,你永远无法知道有多少次 updateConstraints 可能会被调用。

Doubling constraints is very inefficient, you can never know how many times updateConstraints might be called.

由于这个博客帖子显示,使用一个标志是最简单,最有效的方法的处理这个问题。这就是我如何处理这个自己。

As this blog post shows, using a flag is the simplest, most efficient way of dealing with this problem. That's how I deal with this myself.

作为一个方面说明,你提到那里的存在你有没有考虑过的一个真棒方式。大部分的时间,最简单的方法是最真棒方式。 :)

As a side note, you mention of there existing an awesome way that you have not thought of yet. Most of the times, the simplest way IS the most awesome way. :)

这篇关于自动布局和放大器;程序的限制:如何应对updateConstraints射击多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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