先日このようなUIPageViewControllerに関する質問をさせていただき、UIContainer Viewを使うと良いのではないかという回答をいただきました。
現在下記のようなコードで進めているのですが、ビルドをしようとすると「UICollectionView must be initialized with a non-nil layout parameter」と出てしまいます。
コードの問題点等お分かりになる方がいらっしゃいましたら教えていただけますか。
Swift
1import UIKit 2 3class ViewController: UIViewController, UICollectionViewDelegateFlowLayout { 4 5 let pageList = ["first", "second", "third"] 6 var page:Int = 0 7 8 @IBOutlet weak var collectionView = UICollectionView() 9 let labelheight:CGFloat = 60 10 11 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 collectionView!.dataSource = self 17 collectionView!.delegate = self 18 19 // collectionViewレイアウト作成 20 CollectionViewLayoutSet() 21 22 // collectionViewその他設定 23 collectionView!.showsHorizontalScrollIndicator = false 24 collectionView!.showsVerticalScrollIndicator = false 25 collectionView!.delaysContentTouches = false 26 27 28 view.addSubview(collectionView!) 29 30 self.view.subviews 31 .filter{ $0.isKind(of: UIScrollView.self) } 32 .forEach{($0 as! UIScrollView).delegate = self } 33 } 34 35 func CollectionViewLayoutSet(){ 36 let flowLayout = UICollectionViewFlowLayout() 37 flowLayout.minimumInteritemSpacing = 0.0 38 flowLayout.minimumLineSpacing = 0.0 39 flowLayout.scrollDirection = .horizontal 40 flowLayout.itemSize = CGSize(width:viewframewidth / 3 , height:CGFloat(labelheight)) 41 let rec = CGRect(x: 0.0, y:statusBarHeight , width:viewframewidth , height: labelheight) 42 collectionView! = UICollectionView(frame: rec, collectionViewLayout: flowLayout) 43 44 } 45 46} 47 48extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource { 49 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 50 return pageList.count 51 } 52 53 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 54 collectionView.register(cellType:tab.self) 55 let cell: tab = collectionView.dequeueReusableCell(with: tab.self, for: indexPath) 56 cell.Set(Text: pageList[indexPath.row]) 57 return cell 58 } 59 60 61} 62 63extension ViewController { 64 func scrollViewDidScroll(_ scrollView: UIScrollView) { 65 if(scrollView.className == "UICollectionView"){ 66 return 67 } 68 69 let LeftRightJug = viewframewidth - scrollView.contentOffset.x 70 if(LeftRightJug == 0){ 71 return 72 } 73 let Move = scrollView.contentOffset.x / 3 74 let first:Int = LeftRightJug > 0 ? 1 : 0 75 let End:Int = LeftRightJug > 0 ? (pageList.count - 1) : (pageList.count - 2) 76 if(first < page && page < End) { 77 let pageWidth = CGFloat(page - 2) * viewframewidth 78 let TabPageWidth = pageWidth / 3 79 80 collectionView!.contentOffset = CGPoint(x: Move + TabPageWidth, y:0) 81 } 82 } 83}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/17 08:25
2019/06/18 08:40