이 코드를 확인하십시오. 달성하고자하는 작업을 정확하게 수행하고 있습니다.
func setupScrollView()
{
//loop for
for i in 0..<3 {
//we instantiate our viewController from storyboard
let news2 = self.storyboard?.instantiateViewController(withIdentifier: "NewsMasterViewController") as! NewsMasterViewController
//we adjust the frame according
news2.view.frame = CGRect(x: CGFloat(i) * self.scrollView.frame.size.width + CGFloat(MasterViewController.margin), y: 0, width: self.scrollView.frame.size.width - CGFloat(MasterViewController.margin * 2), height: self.scrollView.frame.size.height)
//we call layoutSubviews for constraints adjustments
news2.view.layoutSubviews()
self.addChildViewController(news2)
//added the view of my viewController "news2" to scrollView
self.scrollView.addSubview(news2.view)
news2.didMove(toParentViewController: self)
//added the viewController "news2" to my array of viewControllers
newsMasterViewControllers.append(news2)
}
//adjusted contentSize of scrollView according the number of viewControllers added
self.scrollView.contentSize = CGSize(width: self.view.frame.size.width * 3, height: self.scrollView.frame.size.height);
}
나는 이것이 당신
컨테이너 뷰에서보세요 도움이되기를 바랍니다. 아이디어는 contentView (UIView)를 만들고 하위 UIViewController를 현재 ViewController에 추가 한 다음 하위 ViewController의 뷰를 내용보기에 추가한다는 것입니다. – uti0mnia