2016-04-04 56 views
0

我需要一些幫助與UIScrollView。我在這個加載內容: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正在加載。你們有誰能告訴我我在這裏做錯了什麼?

回答

0

我的問題是動態添加的視圖的大小爲零,所以在設置viewController的大小(在createViewController(index:i)方法中創建的大小)爲屏幕大小後,像一個魅力。

希望有一天能幫助別人。