简体   繁体   English

UIScrollView不显示动态添加的内容

[英]UIScrollView not displaying dynamically added content

I need some help with the UIScrollView. 我需要有关UIScrollView的帮助。 I am loading content in this: 我正在此加载内容:

for i in _start ..< _loopPages {

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

and this is the function that calculates the size of the view: 这是计算视图大小的函数:

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()
}

The first 10 views are loaded and displayed as they should, but when I load the next 10 views all I get is white space. 前十个视图将按原样加载并显示,但是当我加载后十个视图时,我得到的只是空白。 If I change the screen orientation the pages are displayed, but if I don't all I get is white screens. 如果更改屏幕方向,则会显示页面,但如果不正确,则显示的是白屏。 I can scroll through the screens, the size of the scrollview is updated but the content is not displayed. 我可以在屏幕上滚动,滚动视图的大小已更新,但内容未显示。 When the user scrolls to page 10 the next 10 are being loaded. 当用户滚动到第10页时,将加载下一个10。 Can any of you tell me what am I doing wrong here. 谁能告诉我我在做什么错。

My problem was that the dynamically added views had size of zero, so after i set the size of the viewController (the one created in the createViewController(index: i) method) to be the size of the screen, everything worked like a charm. 我的问题是动态添加的视图的大小为零,因此在我将viewController的大小(在createViewController(index:i)方法中创建的那个)设置为屏幕大小之后,所有内容都像一个超级按钮一样工作。

Hope it helps somebody someday. 希望有一天能对别人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM