【问题标题】:iOS constraints not working in UIScrollViewiOS 约束在 UIScrollView 中不起作用
【发布时间】:2015-09-17 23:17:55
【问题描述】:

我有一个应用程序在 UIScrollView 中布局子视图控制器的视图。视图是使用 storyboard.instantiateViewControllerWithIdentifier() 从情节提要创建的。问题是如果视图没有显示在屏幕上,约束不能正确定位视图。奇怪的是,当您与视图交互(按下按钮或开始滚动)时,约束会自行修复。 setNeedsDisplay(), setNeedsLayout, updateConstraints 等函数不起作用。

编辑:
用于将视图加载到 UIScrollView 的代码

for i in 0..<viewControllers.count {
    addChildViewController(viewControllers[i])
    viewControllers[i].view.frame = CGRect(x: CGFloat(i) * scrollView.bounds.size.width, y: 0, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)

    scrollView.addSubview(viewControllers[i].view)
}

【问题讨论】:

  • 什么时候添加这个视图?有时您必须在 viewDidAppear 而不是 viewDidLoad 方法上执行此操作。添加一些代码
  • 从我加载视图的位置更改没有任何效果。

标签: ios swift


【解决方案1】:

经过一些测试,我发现这是 SDK 中的一个错误,iOS 9 修复了这个错误。我发现只有一种方法可以解决 iOS 8 上的问题。那就是在视图作为子视图添加到滚动视图之前设置滚动视图的内容偏移量。用于加载视图以滚动视图的代码是:

for i in 0..<viewControllers.count {
    scrollView.contentOffset.x = CGFloat(i) * view.bounds.size.width

    addChildViewController(viewControllers[i])
    viewControllers[i].view.frame = CGRect(x: CGFloat(i) * scrollView.bounds.size.width, y: 0, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)

    scrollView.addSubview(viewControllers[i].view)
}

scrollView.contentOffset.x = 0.0

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多