【问题标题】:Container View Not Moving Off Screen容器视图未移出屏幕
【发布时间】:2015-03-20 22:38:00
【问题描述】:

我有一个容器视图,我最初希望它位于屏幕底部。这应该很容易,但我似乎缺少一些基本的东西。我以前做过类似的事情,方法是将控件从屏幕左侧移开:

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

我有一个非常简单的故事板设置:

我的视图控制器看起来像这样:

class ViewController: UIViewController {

    @IBOutlet var containerView: UIView!

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        containerView.center.y += view.bounds.height
    }
}

运行时,容器视图正好在故事板中显示的位置,并且不在屏幕外。即使将 containerView 的 center.y 设置为特定值(例如 containerView.center.y = 50)也无济于事。我可以让它在屏幕上炫耀的唯一方法是在 viewDidAppear 中添加动画:

override func viewDidAppear(animated: Bool) {
   super.viewDidAppear(animated)
   UIView.animateWithDuration(0, animations: {self.containerView.center.y += self.view.bounds.height})
}

但这似乎只是一种解决方法,而不是正确的方法。我已经处理了几个小时,阅读博客,阅读其他 Stackoverflow 问题,但我找不到解决方案。我对容器没有约束,尽管我确实用约束对其进行了测试,但这并没有什么不同。任何帮助表示赞赏。

【问题讨论】:

    标签: ios swift uiview uiviewanimation


    【解决方案1】:

    主视图的 `layoutSubviews 可能在您重新定位容器视图后被调用,因此它将默认为故事板中指定的位置。

    正确的做法是在情节提要中有适当的布局约束,为底部边缘约束创建一个出口并设置此约束的constant 属性。

    【讨论】:

    • 谢谢!我将 viewWillAppear 修改为 containerViewBottomConstraint.constant = -containerView.frame.height,现在它可以按预期工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多