我最近遇到动画UITableView
的问题。事实证明,问题是由自动布局限制引起的。我使用以下代码禁用了UITableView
的自动布局:
myTableView.translatesAutoresizingMaskIntoConstraints = YES;
这解决了这个问题,但现在我每次动画UITableView
时都会打破框架上的约束并得到以下警告。
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)
(
"<NSIBPrototypingLayoutConstraint:0x170a95db0 'IB auto generated at build time for view with fixed frame' V:|-(538)-[UITableView:0x140330c00] (Names: '|':UIView:0x170199640 )>",
"<NSIBPrototypingLayoutConstraint:0x170c9d010 'IB auto generated at build time for view with fixed frame' V:[UITableView:0x140330c00(220)]>",
"<NSAutoresizingMaskLayoutConstraint:0x175e850a0 h=--& v=--& UITableView:0x140330c00.midY == + 458>",
"<NSAutoresizingMaskLayoutConstraint:0x175c97b10 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x170199640] (Names: '|':UITransitionView:0x143942020 )>"
)
Will attempt to recover by breaking constraint
<NSIBPrototypingLayoutConstraint:0x170a95db0 'IB auto generated at build time for view with fixed frame' V:|-(538)-[UITableView:0x140330c00] (Names: '|':UIView:0x170199640 )>
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.
我需要使用translatesAutoresizingMaskIntoConstraints
让我的动画工作,但我想摆脱这个讨厌的警告。有没有办法在不删除translatesAutoresizingMaskIntoConstraints = YES
的情况下消除此警告?
根据评论进行修改:
因此UITableView
的宽度为320,高度为220.它覆盖了屏幕的一半,可以最小化或最大化。视图初始化为UITableView
处于最小化状态,只有顶部标题显示在屏幕底部。当用户点击节标题中的按钮时,UITableView
从底部向上展开,以覆盖可视屏幕的一半。 UITableView
的扩展和最小化是UITableView
动画的地方。当用户单击最大化/最小化按钮时,UITableView
帧的y分量将更改为相应地移动它。这是我用来为表视图设置动画的代码:
UIImage *downImage = [UIImage imageNamed:@"arrowDown.png"];
UIImage *upImage = [UIImage imageNamed:@"arrowUp.png"];
UIButton *btnExpandCompress = (UIButton *)sender;
[btnExpandCompress setEnabled:NO];
if (showingTable)
{
[UIView animateWithDuration:0.7 animations:^{
CGRect frame = animatedTable.frame;
frame.origin.y += animatedTable.frame.size.height - 30;
animatedTable.frame = frame;
} completion:^(BOOL finished) {
showingTable = NO;
[btnExpandCompress setEnabled:YES];
[btnExpandCompress setBackgroundImage:upImage forState:UIControlStateNormal];
}];
}
else
{
[UIView animateWithDuration:0.7 animations:^{
CGRect frame = animatedTable.frame;
frame.origin.y -= animatedTable.frame.size.height - 30;
animatedTable.frame = frame;
} completion:^(BOOL finished) {
showingTable = YES;
[btnExpandCompress setEnabled:YES];
[btnExpandCompress setBackgroundImage:downImage forState:UIControlStateNormal];
}];
}
从上面可以看出,我所做的就是改变框架的y分量。最初UITableView
的框架具有583的y分量,在原始约束警告<NSIBPrototypingLayoutConstraint:0x170a95db0 'IB auto generated at build time for view with fixed frame' V:|-(538)-[UITableView:0x140330c00] (Names: '|':UIView:0x170199640 )>
中提到。我没有为UITableView
设置任何其他约束,只需将其从对象库拖动并将其移动到其初始最小化位置即可。我猜测通过更改框架y组件我打破了默认设置的固定约束。我希望这些额外信息有所帮助。我该如何解决这个问题?
答案 0 :(得分:0)
您的tableview存在问题
您已为tableview的高度设置了多个值
约束-538高度tableview
。
正如您为tableview设置了固定高度