改变视图的尺寸时,编程自动布局似乎不工作 [英] Autolayout seems not working when changing view dimensions programmatically

查看:141
本文介绍了改变视图的尺寸时,编程自动布局似乎不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有设定一些限制一个UITableViewCell的自定义子类。

问题是,当我试图改变一个视图的大小是这样的:

 的CGRect FRM = CGRectMake(0,0,someValue中,30);
cell.indentView.frame = FRM;

其他视图,这取决于cell.indentView宽度,不动的。

什么可以这样?

这code将工作:

  //枚举所有约束
对于(NSLayoutConstraint *在cell.indentView.constraints约束){
    //找到约束这一观点,并与'宽'属性
    如果(constraint.firstItem == cell.indentView&放大器;&安培; constraint.firstAttribute == NSLayoutAttributeWidth)
    {
        //约束的宽度增加
        constraint.constant = someValue中;
        打破;
    }
}


解决方案

这是因为你不能只更改具有自动布局约束(因为限制规定的位置,而不是手动设置架<对象的框架/ code>属性... 将被重置为约束属性重新应用)。见<一href=\"http://stackoverflow.com/questions/14189362/animating-an-image-view-to-slide-upwards/14190042#14190042\">this松散的联系后其所演示了如何正确地调整制约移动视图。 (在这种情况下,我们的动画,这是不是这里的问题,但它确实说明了如何怎么一调整制约影响的运动的UIView

在总之,如果你想移动的UIView 自动布局中,你不能试图改变的,而是调整约束自己。如果创建约束自己 IBOutlet中引用这是简化的,在这一点上,你可以调整的 NSLayoutConstraint


更新:

叶戈尔推荐枚举的约束和编辑该匹配所述一个有问题的之一。这一伟大工程,但就个人而言,我preFER有一个 IBOutlet中的具体约束我想编辑,而不是列举他们狩猎一个问题。如果你有一个 IBOutlet中名为 indentViewWidthConstraint 的宽度 NSLayoutConstraint indentView ,你可以简单地话:

  self.indentViewWidthConstraint.constant = newIndentConstantValue;

I have custom subclass of a UITableViewCell with some constraints set.

Problem is when I try to change size of one view like this:

CGRect frm = CGRectMake(0, 0, someValue, 30);
cell.indentView.frame = frm;

other views, which depend on cell.indentView width, are not moving at all.

What can be the case?

This code will work:

// enumerate over all constraints
for (NSLayoutConstraint *constraint in cell.indentView.constraints) {
    // find constraint on this view and with 'width' attribute
    if (constraint.firstItem == cell.indentView && constraint.firstAttribute == NSLayoutAttributeWidth)
    {
        // increase width of constraint
        constraint.constant = someValue;
        break;
    }
}

解决方案

It's because you cannot just change the frame of an object that has autolayout constraints (because constraints dictate the locations, not manually set frame properties ... the frame properties will be reset as constraints are reapplied). See this loosely related post in which it is demonstrated how to properly adjust a constraint to move a view. (In that case, we're animating, which is not the issue here, but it does demonstrate how how one adjusts a constraint to effect the movement of a UIView.)

In short, if you want to move a UIView within autolayout, you must not try to change the frame, but rather adjust the constraints themselves. This is simplified if you create IBOutlet references for the constraints themselves, at which point, you can adjust the constant of the NSLayoutConstraint.


Update:

Egor recommended enumerating the constraints and editing the one that matched the one in question. That works great, but personally, I prefer to have an IBOutlet for the specific constraint I want to edit rather than enumerating them and hunting the one in question. If you had an IBOutlet called indentViewWidthConstraint for the width NSLayoutConstraint of the indentView, you could then simply:

self.indentViewWidthConstraint.constant = newIndentConstantValue;

这篇关于改变视图的尺寸时,编程自动布局似乎不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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