无法更改表格视图框架

Posted

技术标签:

【中文标题】无法更改表格视图框架【英文标题】:Can't change tableview frame 【发布时间】:2016-08-28 22:07:47 【问题描述】:

我只想在 viewwillappear 中更改 tableview 框架,使用 easyPeasy 进行自动布局。

我在一个视图控制器中有collectionview 和tableview。我正在使用它们。但是在第一次打开时我隐藏collectionview 并试图将tableview y 点移动到0。

override func viewWillAppear(animated: Bool) 
        super.viewWillAppear(true)
        self.collectionView.hidden = true
        self.tableView <- [
            Top(0).to((self.navigationController?.navigationBar)!, .Bottom)
        ]
    

它给了我这个错误。

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. 
(
    "<NSLayoutConstraint:0x14c06f370 UIImageView:0x14c1c2d30.width == 40>",
    "<NSLayoutConstraint:0x14c1e5ff0 UIImageView:0x14c1c2d30.width == 22>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c06f370 UIImageView:0x14c1c2d30.width == 40>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
nil
2016-08-29 01:06:37.034 Bons[985:280487] 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. 
(
    "<NSLayoutConstraint:0x14c0e29d0 UIImageView:0x14c1e9ee0.width == 40>",
    "<NSLayoutConstraint:0x14c1e7f00 UIImageView:0x14c1e9ee0.width == 22>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c0e29d0 UIImageView:0x14c1e9ee0.width == 40>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
nil
2016-08-29 01:06:37.044 Bons[985:280487] 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. 
(
    "<NSLayoutConstraint:0x14c0f5c50 UIImageView:0x14c0f4a10.width == 40>",
    "<NSLayoutConstraint:0x14c1e7fc0 UIImageView:0x14c0f4a10.width == 22>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c0f5c50 UIImageView:0x14c0f4a10.width == 40>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
nil
2016-08-29 01:06:37.053 Bons[985:280487] 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. 
(
    "<NSLayoutConstraint:0x14c0f5930 UILabel:0x14c0f5020.centerY == UITableViewCellContentView:0x14c0f3c60.centerY>",
    "<NSLayoutConstraint:0x14c0f59d0 UIImageView:0x14c0f4a10.top == UIImageView:0x14c0f5600.top + 5>",
    "<NSLayoutConstraint:0x14c0f5a70 UIImageView:0x14c0f4a10.centerY == UILabel:0x14c0f5020.centerY>",
    "<NSLayoutConstraint:0x14c0f5b10 UITableViewCellContentView:0x14c0f3c60.bottomMargin == UIImageView:0x14c0f5600.bottom - 1>",
    "<NSLayoutConstraint:0x14c0f5bb0 UIImageView:0x14c0f5600.height == 38>",
    "<NSLayoutConstraint:0x14c1f5330 UIImageView:0x14c0f4a10.height == 22>",
    "<NSLayoutConstraint:0x14c1f4cc0 UITableViewCellContentView:0x14c0f3c60.height == 54>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c0f5bb0 UIImageView:0x14c0f5600.height == 38>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-08-29 01:06:37.062 Bons[985:280487] 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. 
(
    "<NSLayoutConstraint:0x14c0efad0 UILabel:0x14c0f07b0.centerY == UITableViewCellContentView:0x14c1e85d0.centerY>",
    "<NSLayoutConstraint:0x14c0e25a0 UIImageView:0x14c1e9ee0.top == UIImageView:0x14c0ef890.top + 5>",
    "<NSLayoutConstraint:0x14c0e2640 UIImageView:0x14c1e9ee0.centerY == UILabel:0x14c0f07b0.centerY>",
    "<NSLayoutConstraint:0x14c0e2890 UITableViewCellContentView:0x14c1e85d0.bottomMargin == UIImageView:0x14c0ef890.bottom - 1>",
    "<NSLayoutConstraint:0x14c0e2930 UIImageView:0x14c0ef890.height == 38>",
    "<NSLayoutConstraint:0x14c0f1c90 UIImageView:0x14c1e9ee0.height == 22>",
    "<NSLayoutConstraint:0x14c1f87a0 UITableViewCellContentView:0x14c1e85d0.height == 54>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c0e2930 UIImageView:0x14c0ef890.height == 38>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-08-29 01:06:37.070 Bons[985:280487] 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. 
(
    "<NSLayoutConstraint:0x14c089ce0 UILabel:0x14c0c8050.centerY == UITableViewCellContentView:0x14c06e390.centerY>",
    "<NSLayoutConstraint:0x14c089d80 UIImageView:0x14c1c2d30.top == UIImageView:0x14c071f20.top + 5>",
    "<NSLayoutConstraint:0x14c089e20 UIImageView:0x14c1c2d30.centerY == UILabel:0x14c0c8050.centerY>",
    "<NSLayoutConstraint:0x14c06f230 UITableViewCellContentView:0x14c06e390.bottomMargin == UIImageView:0x14c071f20.bottom - 1>",
    "<NSLayoutConstraint:0x14c06f2d0 UIImageView:0x14c071f20.height == 38>",
    "<NSLayoutConstraint:0x14c1e5560 UIImageView:0x14c1c2d30.height == 22>",
    "<NSLayoutConstraint:0x14c211d80 UITableViewCellContentView:0x14c06e390.height == 54>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c06f2d0 UIImageView:0x14c071f20.height == 38>

【问题讨论】:

任何第三方库都是一把双刃剑。有些事情变得容易,有些事情变得更加困难。调试不起作用的东西通常是非常痛苦的。我的建议是,如果该库的文档对您没有帮助,请放弃该库并使用本机 Apple API 来创建和编辑您的约束。 我没有使用 3rd 方框架并再次尝试。它失败了。给了我同样的输出 【参考方案1】:

问题在于您正试图建立不包含在同一视图中的项目之间的关系。相反,您可以:

override func viewWillAppear(animated: Bool) 
    super.viewWillAppear(true)
    self.collectionView.hidden = true
    self.tableView <- [
        Top(0).to(self.topLayoutGuide))
    ]

【讨论】:

以上是关于无法更改表格视图框架的主要内容,如果未能解决你的问题,请参考以下文章

无法自动更新表格视图中的子值

iOS:无法更改水平堆栈视图中的宽度

无法在 iOS 中滚动表格

无法在自定义表格视图单元格中使 ImageView 成为一个圆圈

无法更改 UIButton 图像

Swift:无法以编程方式编辑自定义表格单元格的属性