2016-12-01 96 views
0

试图把ViewControllers放在一个水平滚动的ScrollView中,我试过了,无论我做什么,每个ViewController的宽度都不正确,无法让每个ViewController都相同。上面的ScrollView工作得很好,用于通过UIImageView进行分页。下面的问题是这个问题,也许有更好的方法来实现这一点。iOS 10 Swift 3 - ViewController里面的ScrollView

希望对此有帮助,代码粘贴在其他人的下方。希望能帮助到你。

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) 
+0

你可以使用pageviewcontroller。 –

+0

pageviewcontroller采取整个页面,这不是我要去的用户 – ahmad

+0

使用容器视图..than和在该容器中,您可以添加pageviewcontroller –

回答

0

可以在契税使用Collection视图和视图控制器。

+0

我可以使用集合视图,但那里已经有一个,所以我试图避免。我只是难以理解为什么这个问题正在发生,而且如果滚动视图是全屏的话,它已经完美地工作了。 – ahmad