2017-12-25 14 views
0

내 thumbnailImage에 CollectionView 셀의 탭 제스처가 있습니다. 스토리 보드를 사용하지 않고 다른 View Controller에 푸시/프리젠 테이션/프레젠테이션을 할 때 작동했습니다. 이제 스토리 보드를 구현 했으므로 더 이상 작동하지 않으며 많은 다른 솔루션이며 전환이 없습니다. 스토리 보드에 클래스를 설정했지만 모든 것이 프로그래밍 방식으로 수행됩니다. 현재 ViewController에서 CollectionView 셀로 전환 할 수있는 방법이 있는지 궁금합니다. 스토리 보드의CollectionView Cell segue

그림 :

enter image description here

이것은 시뮬레이터의 모습입니다 :

enter image description here

import UIKit 

    class Cell: UICollectionViewCell{ 

     override init(frame: CGRect) { 
      super.init(frame: frame) 
      setupViews() 

      let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(Tap(recognizer:))) 
      thumbnailImage.isUserInteractionEnabled = true 
      thumbnailImage.addGestureRecognizer(tapRecognizer) 

     } 

     func setupViews(){ 
      addSubview(thumbnailImage) 
      addSubview(separatorView) 
      addSubview(brandName) 
      addSubview(Priceing) 
      addSubview(model) 


      addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: thumbnailImage) 
      addConstraintsWithFormat(format: "H:|[v0]|", views: separatorView) 

      //Vertical Contsraint 
      addConstraintsWithFormat(format: "V:[v0(200)]-87-[v1(1)]|", views: thumbnailImage, separatorView) 
      addConstraintsWithFormat(format: "V:[v0(20)]", views: brandName) 
      addConstraintsWithFormat(format: "V:[v0(20)]", views: Priceing) 
      addConstraintsWithFormat(format: "V:[v0(20)]", views: model) 

      addConstraint(NSLayoutConstraint(item: brandName, attribute: .top, relatedBy: .equal, toItem: thumbnailImage, attribute: .bottom, multiplier: 1, constant: 8)) 
      addConstraint(NSLayoutConstraint(item: brandName, attribute: .right, relatedBy: .equal, toItem: thumbnailImage, attribute: .right, multiplier: 1, constant: 0)) 
      addConstraint(NSLayoutConstraint(item: brandName, attribute: .left, relatedBy: .equal, toItem: thumbnailImage, attribute: .left, multiplier: 1, constant: 0)) 

      addConstraint(NSLayoutConstraint(item: Priceing, attribute: .top, relatedBy: .equal, toItem: brandName, attribute: .bottom, multiplier: 1, constant: 4)) 
      addConstraint(NSLayoutConstraint(item: Priceing, attribute: .right, relatedBy: .equal, toItem: brandName, attribute: .right, multiplier: 1, constant: 0)) 
      addConstraint(NSLayoutConstraint(item: Priceing, attribute: .left, relatedBy: .equal, toItem: brandName, attribute: .left, multiplier: 1, constant: 0)) 

      addConstraint(NSLayoutConstraint(item: model, attribute: .top, relatedBy: .equal, toItem: Priceing, attribute: .bottom, multiplier: 1, constant: 4)) 
      addConstraint(NSLayoutConstraint(item: model, attribute: .right, relatedBy: .equal, toItem: Priceing, attribute: .right, multiplier: 1, constant: 0)) 
      addConstraint(NSLayoutConstraint(item: model, attribute: .left, relatedBy: .equal, toItem: Priceing, attribute: .left, multiplier: 1, constant: 0)) 
     } 

     let separatorView: UIView = { 
      let view = UIView() 
      view.backgroundColor = UIColor(displayP3Red: 230/255, green: 230/255, blue: 230/255, alpha: 1) 
      view.translatesAutoresizingMaskIntoConstraints = false 
      return view 
     }() 

     let brandName: UILabel = { 
      let label = UILabel() 
      label.translatesAutoresizingMaskIntoConstraints = false 
      label.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211) 
      label.numberOfLines = 2 
      return label 
     }() 

     let model: UILabel = { 
      let label = UILabel() 
      label.translatesAutoresizingMaskIntoConstraints = false 
      label.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211) 
      label.numberOfLines = 1 
      return label 
     }() 

     let Priceing: UILabel = { 
      let textView = UILabel() 
      textView.translatesAutoresizingMaskIntoConstraints = false 
      textView.numberOfLines = 1 
      textView.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211) 
      return textView 
     }() 

     let thumbnailImage: UIImageView = { 
      let image = UIImageView() 
      image.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211) 
      image.contentMode = .scaleAspectFill 
      image.clipsToBounds = true 
      return image 
     }() 

     @objc func Tap(recognizer: UITapGestureRecognizer){ 
      let vc = DescriptionViewController() 
    //  let nc = self.window?.rootViewController?.navigationController 
    //  let transition = CATransition() 
    //  transition.duration = 0.3 
    //  transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) 
    //  transition.type = kCATransitionMoveIn 
    //  transition.subtype = kCATransitionFromTop 
    //  nc?.navigationController?.view.layer.add(transition, forKey: nil) 
      self.window?.rootViewController?.navigationController?.pushViewController(vc, animated: false) 
     } 



     required init?(coder aDecoder: NSCoder) { 
      fatalError("init(coder:) has not been implemented") 
     } 
    } 
+0

'UICollectionViewCell' 클래스를 사용하는 대신 네비게이션을 위해 UIViewController 클래스를 사용하십시오. –

+0

@RichieRich 그 thumbnailImage에 대한 구체적인 제스처는 어떻게 UIViewController에서 할 수 있습니까? UIViewController에 탭 제스처가 있다면 UIViewController 클래스에 CollectionView와 UIButton 만 있기 때문에 어떻게 작동할까요? –

+0

데모 프로젝트를 공유 할 수 있다면 도와 드리겠습니다. –

답변

0

당신의 UIView 부모를 찾기 UIViewController

extension UIView { 

    var parentViewController: UIViewController? { 
     var parentResponder: UIResponder? = self 
     while parentResponder != nil { 
      parentResponder = parentResponder!.next 
      if let viewController = parentResponder as? UIViewController { 
       return viewController 
      } 
     } 
     return nil 
    } 
} 

프로젝트에서이 확장을 추가하고 그

if let vc = self.parentViewController as? YourPresentedViewController{ 
     let pushVc = DescriptionViewController() 
     vc.navigationController?.pushViewController(pushVc, animated: true) 
    } 

노트 같은 호출이 확장을 사용 : - UITableViewYourPresentedViewController에 추가 그래서 내있는 tableview의 parentViewController는 YourPresentedViewController이다.

+0

if 문이 호출되지만 푸시되지 않습니다. pushVC에서 멈 춥니 다. –

+0

pushVc는 대상보기 컨트롤러를 의미합니다. DescriptionViewController – AshvinGudaliya

+0

그게 내가 한 일이고 didSelectAt indexPath 함수로 설정하고 ParentViewController MainfeedViewController로 설정했습니다. –