首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向单元格动态添加子视图将导致布局约束错误输出。

向单元格动态添加子视图将导致布局约束错误输出。
EN

Stack Overflow用户
提问于 2015-05-26 00:28:03
回答 2查看 519关注 0票数 0

我有一个表格单元格视图,它由几个按钮和一些细节组成。细节被隐藏,直到其中一个按钮被触摸,而当另一个按钮被触摸时,细节被移除。我在使用自动布局。

下面是添加控件和配置约束的代码(控件已被重命名以保护我的客户端)。代码是C#,但是等价的Obj/Swift应该相当明显:

代码语言:javascript
运行
复制
this.ContentView.AddSubviews(this.firstButton, this.secondButton);
this.detailView.AddSubviews(this.textField1, this.textField2, this.textField3, this.textField4, this.textField5);

this.ContentView.ConstrainLayout(() =>
    this.firstButton.Top() == this.ContentView.Top() + Layout.StandardSiblingViewSpacing &&
    this.firstButton.Bottom() <= this.ContentView.Bottom() - Layout.StandardSiblingViewSpacing &&
    this.firstButton.Left() == this.ContentView.Left() + Layout.StandardSuperviewSpacing &&
    this.secondButton.Top() == this.firstButton.Top() &&
    this.secondButton.Bottom() <= this.ContentView.Bottom() - Layout.StandardSiblingViewSpacing &&
    this.secondButton.Left() == this.firstButton.Left());

this.detailView.ConstrainLayout(() =>
    this.textField1.Top() == this.detailView.Top() &&
    this.textField1.Left() == this.detailView.Left() &&
    this.textField1.Right() == this.detailView.Right() &&
    this.textField2.Top() == this.textField1.Bottom() + Layout.StandardSiblingViewSpacing &&
    this.textField2.Left() == this.textField1.Left() &&
    this.textField2.Right() == this.textField1.Right() &&
    this.textField3.Top() == this.textField2.Bottom() + Layout.StandardSiblingViewSpacing &&
    this.textField3.Left() == this.textField2.Left() &&
    this.textField3.Right() == this.textField2.Right() &&
    this.textField4.Top() == this.textField3.Bottom() + Layout.StandardSiblingViewSpacing &&
    this.textField4.Left() == this.textField3.Left() &&
    this.textField4.Right() == this.textField3.Right() &&
    this.textField5.Top() == this.textField4.Bottom() + Layout.StandardSiblingViewSpacing &&
    this.textField5.Bottom() <= this.detailView.Bottom() - Layout.StandardSiblingViewSpacing &&
    this.textField5.Left() == this.textField4.Left() &&
    this.textField5.Right() == this.textField4.Right());

detailView中动画的代码如下所示:

代码语言:javascript
运行
复制
private void AnimateDetailsIn()
{
    this.ContentView.AddSubview(this.detailView);
    this.ContentView.ConstrainLayout(() =>
        this.detailView.Top() == this.ContentView.Top() + Layout.StandardSiblingViewSpacing &&
        this.detailView.Bottom() <= this.ContentView.Bottom() - Layout.StandardSiblingViewSpacing &&
        this.detailView.Left() == this.secondButton.Right() + Layout.StandardSiblingViewSpacing &&
        this.detailView.Right() == this.ContentView.Right() - Layout.StandardSuperviewSpacing);

    var tableView = this.FindParent<UITableView>();

    if (tableView == null)
    {
        return;
    }

    tableView.BeginUpdates();
    tableView.EndUpdates();
}

每当调用AnimateDetailsIn时,屏幕上的一切看起来都很好,但是我在输出中看到了这个错误消息:

代码语言:javascript
运行
复制
Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fccb670 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fd8efd0(36)]>",
    "<NSLayoutConstraint:0x7fd94330 V:|-(0)-[UITextField:0x7fd905a0]   (Names: '|':UIView:0x7fd90050 )>",
    "<NSLayoutConstraint:0x7fd943c0 V:[UITextField:0x7fd905a0]-(8)-[UITextField:0x7fd90e90]>",
    "<NSLayoutConstraint:0x7fd94450 V:[UITextField:0x7fd90e90]-(8)-[UITextField:0x7fd91720]>",
    "<NSLayoutConstraint:0x7fd944e0 V:[UITextField:0x7fd91720]-(8)-[UITextField:0x7fd91fb0]>",
    "<NSLayoutConstraint:0x7fd94570 V:[UITextField:0x7fd91fb0]-(8)-[UITextField:0x7fd92850]>",
    "<NSLayoutConstraint:0x7fd945a0 UITextField:0x7fd92850.bottom <= UIView:0x7fd90050.bottom - 8>",
    "<NSLayoutConstraint:0x7fdaa080 V:|-(8)-[UIView:0x7fd90050]   (Names: '|':UITableViewCellContentView:0x7fd8efd0 )>",
    "<NSLayoutConstraint:0x7fdaa200 UIView:0x7fd90050.bottom <= UITableViewCellContentView:0x7fd8efd0.bottom - 8>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fd94570 V:[UITextField:0x7fd91fb0]-(8)-[UITextField:0x7fd92850]>

为了排除故障,我尝试将detailView包含在视图层次结构中,从启动和删除动画。这很好(没有错误输出,一切都显示正确)。所以很明显,我在这里所做的事情有些不太正确。有人能告诉我如何在使用自动布局时动画化我的detailView而不获得错误输出吗?

EN

Stack Overflow用户

发布于 2015-05-26 01:24:06

在将子视图添加到contentview之后,您需要在每个子视图上将‘translatesautosizingmaskinto约束’设置为NO。你在什么地方这么做吗?否则,系统会添加一些您不想要的约束。如果你还没有,你能试试这个吗?

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30447665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档