【问题标题】:UIScrollView not displaying dynamically added contentUIScrollView 不显示动态添加的内容
【发布时间】:2016-04-04 19:08:35
【问题描述】:

我需要一些有关 UIScrollView 的帮助。我正在加载内容:

for i in _start ..< _loopPages {

        let articleController = createViewController(index: i)
        viewControllers.append(articleController)
        articleController.didMoveToParentViewController(self)
        scrollView.addSubview(articleController.view)
        articleController.view.setNeedsDisplay()
    }

这是计算视图大小的函数:

func calculateScrollViewSize(){
for i in _start ..< numberOfArticles {
        let articleController = viewControllers[i]
        articleController.view.frame = view.bounds
        let x = i * Int(articleController.view.bounds.size.width)
        print("RECT: ", CGRect(origin: CGPoint(x: x, y: 0), size: view.bounds.size))
        articleController.view.frame = CGRect(origin: CGPoint(x: x, y: 0), size: view.bounds.size)
    }

    let contentWidth = CGFloat(numberOfArticles) * view.bounds.size.width
    let contentHeight = view.bounds.size.height
    scrollView.contentSize = CGSize(width: contentWidth, height: contentHeight)

    let offsetX = currentPage * Int(view.bounds.size.width)
    let offsetY = 0
    scrollView.contentOffset = CGPoint(x: offsetX, y: offsetY)
    //scrollView.contentInset.top = 0

    scrollView.setNeedsDisplay()
}

前 10 个视图按应有的方式加载和显示,但是当我加载接下来的 10 个视图时,我得到的只是空白。如果我更改屏幕方向,则会显示页面,但如果我不这样做,我得到的只是白屏。我可以滚动屏幕,滚动视图的大小已更新,但内容不显示。当用户滚动到第 10 页时,正在加载下一个 10。你们谁能告诉我我在这里做错了什么。

【问题讨论】:

    标签: ios iphone swift uiscrollview


    【解决方案1】:

    我的问题是动态添加的视图的大小为零,所以在我将 viewController 的大小(在 createViewController(index: i) 方法中创建的那个)设置为屏幕大小之后,一切都像魅力。

    希望有一天它对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多