0

Instagramのクローンを作成しようとしていて、3つの異なるセルを返すUICollectionViewControllerを使用し、スクロール方向を水平に設定し、私は2つの垂直方向のセルに3つの異なるページを持つことができます私は2つの他のuicollectionviewcellの入れ子になっている1のフィードのために1、私は問題に実行されている時にナビゲーションバーを隠すInstagramにはフィードセルとメッセージングセルのナビゲーションバーが表示されますが、カメラのナビゲーションバーは表示されないためです。以下はmaincollectionviewcontrollerのコードです。UICollectionviewcellを異なるセルサイズで垂直にレンダリングする方法

import UIKit 
import AVFoundation 

class MainViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    let cellID = "cellId" 
    let messageCellID = "messageCellID" 
    let cameraCellID = "cameraCellID" 
    var swipeRight = UISwipeGestureRecognizer() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let titleImage = UIImageView(image: #imageLiteral(resourceName: "Instagram_logo")) 
     titleImage.layer.masksToBounds = true 
     self.navigationItem.titleView = titleImage 

     setupCollectionView() 
     setupSwipeGesture() 
     } 

// //Swipe right to get camera 
// func setupSwipeGesture() { 
//  swipeRight.direction = .right 
//  self.navigationController?.isNavigationBarHidden = true 
//  let cameraViewController = ViewController() 
//  cameraViewController.transitioningDelegate = self 
//  navigationController?.pushViewController(cameraViewController, animated: true) 
// } 

    func setupSwipeGesture() { 
     print("trying to swipe") 
     swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) 
     self.view.addGestureRecognizer(swipeRight) 
     swipeRight.direction = .right 
    } 

    func swiped(){ 
     print("swipping to get Camera") 
     self.navigationController?.isNavigationBarHidden = true 
     let cameraViewController = ViewController() 
     cameraViewController.transitioningDelegate = self 
     navigationController?.pushViewController(cameraViewController, animated: true) 

    } 


    func setupCollectionView(){ 

     collectionView?.backgroundColor = .white 
     collectionView?.register(MainViewFeedCellCollectionViewCell.self, forCellWithReuseIdentifier: cellID) 
     collectionView?.register(MainViewMessagedFeedCell.self, forCellWithReuseIdentifier: messageCellID) 
     collectionView?.register(MainViewCameraFeed.self, forCellWithReuseIdentifier: cameraCellID) 
     collectionView?.isPagingEnabled = true 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     scrollToMenuIndex(menuIndex: 0) 
    } 

    func goBackToMainPage(){ 
     scrollToMenuIndex(menuIndex: 0) 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if (indexPath.item == 2){ 
      return collectionView.dequeueReusableCell(withReuseIdentifier: messageCellID, for: indexPath) 
     } 

     else if (indexPath.item == 0){ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cameraCellID, for: indexPath) 
      return cell 
     } 

     else if (indexPath.item == 1){ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) 
      return cell 
     } 

     else{ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) 
      return cell 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     print("\(indexPath.row)") 
//  if indexPath.row == 0{ 
//   navigationController?.isNavigationBarHidden = true 
//   return CGSize(width: view.frame.width, height:`  view.frame.height) 
     //} 
     return CGSize(width: view.frame.width, height: view.frame.height - 70) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 

    func scrollToMenuIndex(menuIndex: Int){ 
     let index = IndexPath(item: menuIndex, section: 0) 
     collectionView?.scrollToItem(at: index, at: .centeredHorizontally, animated: true) 

    } 

} 

答えて

0

私が同様の問題を解決するために行ったことは、UIViewControllerにUIScrollViewDelegateを追加することでした。ここでは、あなたが提供したものに基づいた解釈的な答えがあります。一度に1つのUICollectionViewCellしか表示されないため、これはトリックを行う必要があります。私はこれが完了するための最良の方法であるかどうかは分かりませんが、それは私のために働いた:

var visibleCurrentCell: IndexPath? { 
    for cell in self.collectionView.visibleCells { 
     let indexPath = self.collectionView.indexPath(for: cell) 
     return indexPath 
    } 

    return nil 
} 

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
    Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(checkIfCameraIsShowing), userInfo: nil, repeats: false) 
} 

func checkIfCameraIsShowing() { 
    if let visibleCurrCell = visibleCurrentCell, let _ = collectionView.cellForItem(at: visibleCurrCell) as? CameraCollectionViewCell { 
     self.navigationController?.isNavigationBarHidden = true 
    } 
} 
関連する問題