参考サイトをみて実装しようと下記の様にコードを書いてみたのですが全く表示されずに困っています。
サイトではPageContentViewControllerを使っているところを個々にViewControllerを用意して埋め込もうとしています。
それ以外はサイトの通りにやっているつもりなのですが、ViewControllerが表示された時PageViewContorollerが表示されませんし、もちろんスライドしても切り替わることもないです。
どこをどの様に修正すれば良いでしょうか?
よろしくお願いします。
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // コンテナに追加 let pageVC = PageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) let pageViewHeight: CGFloat = 100 pageVC.view.frame = CGRect(x: 0, y: view.bounds.size.height - pageViewHeight, width: view.bounds.size.width, height: pageViewHeight) addChildViewController(pageVC) view.addSubview(pageVC.view) pageVC.didMove(toParentViewController: self) // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } コード
import UIKit class PageViewController: UIPageViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate{ var contentVCs = [UIViewController]() var currentIndex: Int = 0 override func viewDidLoad() { super.viewDidLoad() dataSource = self delegate = self let contentVC = ViewController1() let contentVC2 = ViewController2() contentVCs.append(contentVC) contentVCs.append(contentVC2) setViewControllers([contentVCs[0]], direction: .forward, animated: true, completion: nil) // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { guard let index = contentVCs.index(of: viewController), index != NSNotFound else { return nil } if index > 0 { return contentVCs[index - 1] } else { return contentVCs.last } } func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { guard let index = contentVCs.index(of: viewController), index != NSNotFound else { return nil } if index < contentVCs.count - 1 { return contentVCs[index + 1] } else { return contentVCs.first } } func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { return contentVCs.count } func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { return currentIndex } func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { guard finished else { return } guard let contentVC = pageViewController.viewControllers?.first else { return } guard let index = contentVCs.index(of: contentVC), index != NSNotFound else { return } currentIndex = index } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } コード
あなたの回答
tips
プレビュー