2016-05-23 7 views
3

UIScrollView内のオブジェクトをインスタンス化しようとしているストーリーボード識別子「vc0」と「vc1」の2つのUIViewControllersがあります。 。目標は、SnapChatと同様に、ユーザーが左右にスワイプしてView Controller間をスワイプすることです。ストーリーボードを使用して、UIScrollViewのViewController制約

コードを実行すると、スクロールビューの最初のページには最初のビューコントローラが含まれ、2番目のページには何も含まれません。これは、最初のビューコントローラが2番目のビューコントローラと重なっているためです。 vc0の右端がvc1の左端と一致するように、この問題を修正するために制約を変更するにはどうすればよいですか?

UIScrollViewは、ビューコントローラ内のUIView内に含まれています。関連するすべてのコードを含むviewDidLoad()を次に示します。私が提供すべきその他の有用な情報をお知らせください。

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    let screenWidth = self.view.frame.width 
    let screenHeight = self.view.frame.height 

    let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("vc0") 

    self.addChildViewController(vc0!) 
    self.scrollView.addSubview(vc0!.view) 
    vc0!.didMoveToParentViewController(self) 

    let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("vc1") 

    var frame1 = vc1!.view.frame 
    frame1.origin.x = self.view.frame.size.width 
    vc1!.view.frame = frame1 

    self.addChildViewController(vc1!) 
    self.scrollView.addSubview(vc1!.view) 
    vc1!.didMoveToParentViewController(self) 

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height); 

    //ADD CONSTRAINTS TO vc0 

    vc0!.view.translatesAutoresizingMaskIntoConstraints = false 
    let horizontalConstraint = NSLayoutConstraint(item: vc0!.view, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0) 
    view.addConstraint(horizontalConstraint) 

    let widthConstraint = NSLayoutConstraint(item: vc0!.view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: screenWidth) 
    view.addConstraint(widthConstraint) 

    let heightConstraint = NSLayoutConstraint(item: vc0!.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: screenHeight) 
    view.addConstraint(heightConstraint) 

    //ADD CONSTRAINTS TO vc1 

    vc1!.view.translatesAutoresizingMaskIntoConstraints = false 
    let horizontalConstraint2 = NSLayoutConstraint(item: vc1!.view, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: vc0!.view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0) 
    view.addConstraint(horizontalConstraint2) 

    let widthConstraint2 = NSLayoutConstraint(item: vc1!.view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: screenWidth) 
    view.addConstraint(widthConstraint) 

    let heightConstraint2 = NSLayoutConstraint(item: vc1!.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: screenHeight) 
    view.addConstraint(heightConstraint) 
} 
+1

車輪の再発明する必要はありませんそこにはたくさんのオープンソースライブラリがあります。つまり、https://github.com/goktugyil/EZSwipeController – t0day

+2

@ t0dayです。既存のものよりも優れているなら、ホイールを再現しなければなりません。 :) – BangOperator

答えて

1

私は制約に問題があると思います!

viewscrollviewに追加し、次にadd views of both VCをスクロールビューに直接表示しないでください。

scrollview:上、下、大手、scrollviewに

ビューを末尾:上、下、大手、末尾

ビューのサイズは、今すぐあなたの制約は次のようにする必要がありますscrollview's contentsize

と同じにする必要があります、コンテナ内の垂直中心(中心Y)と固定幅(水平スクロールが必要なため)、垂直スクロールが必要な場合は、コンテナの中心(中心X)と固定高さの水平中心です。

両方のVCから

そして、あなたのビュー:トップ、リード、固定幅、固定高さ、これが役立ちます

・ホープ(幅と高さは、画面サイズと同じにする必要があります):)

+0

これはトリックでした!助けてくれてありがとう。 – Lahav

+0

あなたはようこそ.... :) – Lion

関連する問題