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

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

问题描述

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

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.

我以为,自动布局将自动调整大小以满足的约束。是不是这样的?
我想自动布局意味着不乱用框架rects。我还需要添加子视图时,即使有自动布局来设置初始框架RECT?

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 将自动被自动布局运行时调用。上面的code进去的UIView 的自定义子类。

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

您说的没错,在与自动布局工作,你不想碰帧大小。相反,刚刚更新 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:

<一个href=\"http://stackoverflow.com/questions/17299223/autolayout-uiimageview-with-programatic-re-size-not-following-constraints/17300238#17300238\">Autolayout与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的 - |,这就是你需要的水平维度的一切。自动布局会​​知道大小 MyView的来达到父上海华表示的边界| 。这是pretty凉爽。

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天全站免登陆