如何在屏幕外启动UI TableView?

时间:2015-08-28 14:11:30

标签: ios swift uitableview animation

按下按钮后,我试图从左侧开始显示UITableView。进入主视图后,不应显示表格视图。我还希望表视图只占用屏幕空间的一部分,而不是整个屏幕,否则我会尝试制作单独的视图。

我尝试使用以下方法在tableview中创建IBOutlet并在viewWillAppear()中移动位置:

optionsTableView.center.x -= view.bounds.width

这不起作用,我认为是因为约束系统?

接下来,我尝试将前导和尾随空间限制作为占位符,并在viewDidLoad()块中添加:

//Replace placeholder constraint with a constraint of less than or equal to 0
let leadingSpaceConstraint = NSLayoutConstraint(item: optionsTableView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
optionsTableView.addConstraint(leadingSpaceConstraint)

//Add Trailing space constraint
let trailingSpaceConstraint = NSLayoutConstraint(item: optionsTableView, attribute: NSLayoutAttribute.Trailing,relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
optionsTableView.addConstraint(trailingSpaceConstraint)

但是,这会导致加载视图时出错

2015-08-28 09:00:58.638 ShotMaker[25585:1566797] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb033c48c30 H:|-(<=0)-[UITableView:0x7fb03506e800]   (Names: '|':UIView:0x7fb033c56ca0 )>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2015-08-28 09:00:58.639 ShotMaker[25585:1566797] View hierarchy unprepared for constraint.

我不知道如何解决这个问题,所以任何帮助都会受到赞赏。或者如果你知道一个更好的方法来完成我想要做的事情,我很乐意听到它。 (容器视图?以编程方式实现整个事情?聪明的约束操作?) 此外,如果您知道为什么我第一次尝试移动视图中心不起作用,这也是有用的信息。

最后,我还没有尝试从左侧动画幻灯片,因为我只是通过在屏幕上启动来挂断,但是随时可以提供如何执行此操作的想法,如我真的不知道。

由于

P.S。这是我关于堆栈交换的第一个问题,所以如果我发现任何格式错误或者我可以提供更多信息以获得更多帮助,请告诉我。

1 个答案:

答案 0 :(得分:0)

有多种解决方案。

您可以在Interface Builder中将其设置为离开,将左/前导约束链接到视图控制器并更新它的常量以将表格视图移动到位。

通过在动画块中使用setNeedsLayout更改常量后,您可以轻松地为表视图设置动画: - )

您还可以在viewDidLayoutSubviews中更新表视图的框架,在Interface Builder中使用约束时,仍然在loadView和viewDidLoad中构建表视图的框架。