使用自动布局删除和重新添加子视图 [英] Removing and re-adding a subview with autolayout

查看:24
本文介绍了使用自动布局删除和重新添加子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用自动布局时,我的理解是删除一个子视图(当然同时持有对它的引用),被删除的子视图仍然知道它的自动布局约束.

When using autolayout, my understanding is that removing a subview ( while holding a reference to it of course), the removed subview still knows its autolayout constraints.

但是,稍后将其添加回超级视图时,子视图不再知道其帧大小.相反,它似乎得到了一个零帧.

However, when adding it back to the super view later, the subview no longer knows its frame size. Rather it seems to get a zero frame.

我假设自动布局会自动调整大小以满足约束.不是这样吗?我认为自动布局意味着不要弄乱框架矩形.添加子视图时是否还需要设置初始框架矩形,即使使用自动布局?

I assumed that autolayout would auto size it to meet the constraints. Is that not the case? I thought auto layout meant don't mess with frame rects. Do I still need to set the initial frame rect when adding the subview, even with auto layout?

推荐答案

删除子视图时,与该子视图相关的所有约束都将丢失.如果以后需要再次添加子视图,则必须再次向该子视图添加约束.

When removing the subview, all the constraints that relate to that subview will be lost. If you need to add the subview again later, then you must add constraints to that subview again.

通常,我在自定义子视图中创建约束.例如:

Typically, I create the constraints in my custom subview. For example:

-(void)updateConstraints
{
    if (!_myLayoutConstraints)
    {
        NSMutableArray *constraints = [NSMutableArray array];

       // Create all your constraints here
       [constraints addWhateverConstraints];

       // Save the constraints in an ivar. So that if updateConstraints is called again,
       // we don't try to add them again. That would cause an exception.
       _myLayoutConstraints = [NSArray arrayWithArray:constraints];

       // Add the constraints to myself, the custom subview
       [self addConstraints:_myLayoutConstraints]; 
   }

   [super updateConstraints];
}

updateConstraints 将由 Autolayout 运行时自动调用.上面的代码位于 UIView 的自定义子类中.

updateConstraints will be called automatically by the Autolayout runtime. The code above goes in your custom subclass of UIView.

您说得对,在使用 Autolayout 时,您不想触及帧大小.相反,只需更新 updateConstraints 中的约束.或者,更好的是,设置约束,这样您就不必这样做了.

You're right that in working with Autolayout, you don't want to touch frame sizes. Instead, just update the constraints in updateConstraints. Or, better still, set up the constraints so you don't have to.

查看我对该主题的回答:

See my answer on that topic:

具有程序调整大小的自动布局 UIImageView不遵守约束

您不需要设置初始帧.如果您确实使用 initWithFrame,只需将其设置为 CGRectZero.您的约束将 - 实际上必须 - 详细说明某物应该有多大,或者意味着运行时可以推断出大小的其他关系.

You don't need to set the initial frame. If you do use initWithFrame, just set it to CGRectZero. Your constraints will - in fact must - detail either how big something should be, or other relationships that mean the runtime can deduce the size.

例如,如果您的视觉格式是:@"|-[myView]-|",这就是水平维度所需的一切.Autolayout 会知道 myView 的大小不超过由 | 表示的父 superview 的边界.挺好看的.

For example, if your visual format is: @"|-[myView]-|", that's everything you need for the horizontal dimension. Autolayout will know to size myView to be up to the bounds of the parent superview denoted by |. It's pretty cool.

这篇关于使用自动布局删除和重新添加子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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