2015-11-20 199 views
-1

我想設置「ViewController2」作爲ScrollView中的初始頁面,但更改setContentOffset(CGPointMake(0,0), animated: false)將第一頁設置爲初始,我試着用不同的數字修改它,但它只是弄亂了頁面,任何提示?設置初始ScrollView

import UIKit 

class ViewController: UIViewController { 

@IBOutlet var scrollView: UIScrollView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    scrollView.bounces = false 
    scrollView.pagingEnabled = true 
    scrollView.setContentOffset(CGPointMake(0,0), animated: false) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

override func viewDidLayoutSubviews() { 
    initScrollView() 
} 

func initScrollView(){ 
    let viewController1 = storyboard?.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1 

    viewController1.willMoveToParentViewController(self) 
    viewController1.view.frame = scrollView.bounds 

    let viewController2 = storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2 

    viewController2.willMoveToParentViewController(self) 
    viewController2.view.frame.size = scrollView.frame.size 
    viewController2.view.frame.origin = CGPoint(x: view.frame.width, y: 0) 

    let viewController3 = storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3 

    viewController3.willMoveToParentViewController(self) 
    viewController3.view.frame.size = scrollView.frame.size 
    viewController3.view.frame.origin = CGPoint(x: view.frame.width * 2, y: 0) 

    scrollView.contentSize = CGSize(width: 3 * scrollView.frame.width, height: scrollView.frame.height) 

    scrollView.addSubview(viewController3.view) 
    self.addChildViewController(viewController3) 
    viewController3.didMoveToParentViewController(self) 

    scrollView.addSubview(viewController2.view) 
    self.addChildViewController(viewController2) 
    viewController2.didMoveToParentViewController(self) 

    scrollView.addSubview(viewController1.view) 
    self.addChildViewController(viewController1) 
    viewController1.didMoveToParentViewController(self) 
}} 

回答

0

嘗試取消選擇滾動視圖插圖調整......在VC2:

IB Screen

+0

沒有改變任何東西,是否有一些特殊的代碼與取消選擇滾動視圖插圖一起使用? – Nathannn

0

通過將scrollView.setContentOffset(CGPointMake(375,0), animated: false)在viewDidlayoutSubViews加載下一個視圖,請注意,我用iphone 4 ,7英寸的屏幕,所以視圖控制器的寬度是375.將375更改爲viewController的寬度,例如,如果您想要加載最後一頁,請首先添加375至375等。

override func viewDidLayoutSubviews() { 
    initScrollView() 
    scrollView.setContentOffset(CGPointMake(375,0), animated: false) 
} 
+0

可能會因不同大小的類別而出現問題? – Ron