簡體   English   中英

使用Storyboard在UIScrollView中的ViewController約束

[英]ViewController constraints in UIScrollView, using Storyboard

我有兩個UIViewControllers其中包含Storyboard標識符“vc0”和“vc1”,我試圖在啟用了分頁的UIScrollView實例化對象,使得兩個視圖控制器彼此相鄰。 目標是讓用戶向左和向右滑動以在視圖控制器之間滑動,類似於SnapChat。

運行我的代碼時,scrollview的第一頁包含第一個視圖控制器,而第二個頁面根本不包含任何內容。 我假設這是因為第一個視圖控制器與第二個重疊。 如何修改我的約束(或者根本沒有)來解決這個問題,以便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)
}

我覺得約束有問題!!

您應該向scrollview添加一個view ,然后add views of both VC視圖add views of both VC到該視圖而不是指向scrollview。

視圖的大小應與scrollview's contentsize相同

現在你的約束應該是這樣的:

scrollview:頂部,底部,前導,尾隨

在scrollview中查看:頂部,底部,前導,尾隨, 容器中的垂直中心(中心Y)和固定寬度(因為你想要水平滾動) ,如果想要垂直滾動那么約束應該是(容器中的水平中心(中心X)並固定高度)。

您可以從兩個VC查看:頂部,前導,固定寬度,固定高度(寬度和高度應與屏幕尺寸相同)

希望這會有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM