iOS 10 Swift 3 - ScrollView内的ViewController

时间:2016-12-01 06:02:48

标签: uiviewcontroller uiscrollview swift3 ios10

尝试将ViewControllers放在水平滚动的ScrollView中,无论我做什么,每个ViewController的宽度都不正确,并且不能让每个ViewController都相同。上面的ScrollView工作得很好,用于通过UIImageView进行分页。下面是问题,或许有更好的方法来实现这一目标。

感谢您对此提供的任何帮助,代码将粘贴在其他人的下方。希望它有所帮助。

enter image description here

contentScrollView.translatesAutoresizingMaskIntoConstraints = false

    // Create the three views used in the swipe container view
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let aboutVC: AboutViewController =  storyboard.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
    let categoriesVC: CategoriesViewController =  storyboard.instantiateViewController(withIdentifier: "CategoriesViewController") as! CategoriesViewController
    let reviewsVC: ReviewsViewController =  storyboard.instantiateViewController(withIdentifier: "ReviewsViewController") as! ReviewsViewController

    // Add in each view to the container view hierarchy
    // Add them in opposite order since the view hieracrhy is a stack

    self.addChildViewController(aboutVC)
    self.contentScrollView!.addSubview(aboutVC.view)
    aboutVC.didMove(toParentViewController: self)

    self.addChildViewController(categoriesVC)
    self.contentScrollView!.addSubview(categoriesVC.view)
    categoriesVC.didMove(toParentViewController: self)

    self.addChildViewController(reviewsVC)
    self.contentScrollView!.addSubview(reviewsVC.view)
    reviewsVC.didMove(toParentViewController: self)


    contentScrollView.bringSubview(toFront: contentScrollView)
    // Set up the frames of the view controllers to align
    // with eachother inside the container view
    print("scroll view frame bounds: \(self.pagerScrollView.frame.width)")
    print("view frame bounds: \(self.view.frame.width)")
    print("screen main bounds: \(UIScreen.main.bounds.size.width)")


    let width = self.view.frame.width

    aboutVC.view.frame = CGRect(x: 0, y: 0, width: width, height: contentScrollView.frame.size.height)
    categoriesVC.view.frame = CGRect(x: width, y: 0, width: width, height: contentScrollView.frame.size.height)
    reviewsVC.view.frame = CGRect(x: 2 * width, y: 0, width: width, height: contentScrollView.frame.size.height)

    // Finally set the size of the scroll view that contains the frames
    self.contentScrollView!.contentSize = CGSize(width: 3 * self.pagerScrollView.frame.width, height: contentScrollView.frame.size.height)

1 个答案:

答案 0 :(得分:0)

您可以在契约中使用集合视图和视图控制器。