2017-06-19 124 views
3

我正在致力於ScrollView。目前我在我的ScrollView中拖動圖像,並且我想在ScrollView中至少添加一個新的UIViewController將UIViewController添加到UIScrollView swift 3

那是我還沒有代碼:

self.scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scrollView.frame.height) 
    let scrollviewHeight = self.scrollView.frame.height 
    let scrollviewWidth = self.scrollView.frame.width 

    var imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight)) 
    var imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight)) 

    imgOne.image = UIImage(named: "preview1") 
    imgTwo.image = UIImage(named: "preview2") 

    self.scrollView.addSubview(imgOne) 
    self.scrollView.addSubview(imgTwo) 

    self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 2, height: self.scrollView.frame.height) 
    self.scrollView.isPagingEnabled = true 

設計:

The first one is the root ViewController, where the ScrollView is and the second one I want to add in the ScrollView

+0

哪裏的UIViewController在此代碼 – Krunal

+0

@ Krunal我還沒有添加新的UIViewController,因爲Idk如果以及如何可能。我應該用'@ IBOutlet'來做嗎? – j10

+0

可以這樣做,只是讓我知道,你想在哪裏添加它。分享你的設計。 – Krunal

回答

1
self.scrollView.frame = CGRect(<set frame>) 

var imgOne = UIImageView(frame: CGRect(<set frame>)) 
var imgTwo = UIImageView(frame: CGRect(<set frame>)) 
var vcView = UIView(frame: CGRect(<set frame>)) 
addChildVC(vcView: vcView) 

imgOne.image = UIImage(named: "preview1") 
imgTwo.image = UIImage(named: "preview2") 

self.scrollView.addSubview(imgOne) 
self.scrollView.addSubview(imgTwo) 
self.scrollView.addSubview(vcView) 



self.scrollView.contentSize = CGSize(<set content size>) 
self.scrollView.isPagingEnabled = true 

添加子視圖控制器

func addChildVC(vcView: UIView){ 

let testVC = self.storyboard?.instantiateViewControllerWithIdentifier("testIdentifier") as! TestViewController 
    testVC.view.frame = vcView.bounds 
    vcView.addSubview(testVC.view) 
    self.addChildViewController(testVC) 
    testVC.didMoveToParentViewController(self) 
}