试图把ViewControllers放在一个水平滚动的ScrollView中,我试过了,无论我做什么,每个ViewController的宽度都不正确,无法让每个ViewController都相同。上面的ScrollView工作得很好,用于通过UIImageView进行分页。下面的问题是这个问题,也许有更好的方法来实现这一点。iOS 10 Swift 3 - ViewController里面的ScrollView
希望对此有帮助,代码粘贴在其他人的下方。希望能帮助到你。
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)
你可以使用pageviewcontroller。 –
pageviewcontroller采取整个页面,这不是我要去的用户 – ahmad
使用容器视图..than和在该容器中,您可以添加pageviewcontroller –