I have a tap gesture on my thumbnailImage in the CollectionView cell and it worked when pushed/presented/etc to another View Controller without storyboards now since I implemented the storyboard it doesn't work anymore and I tried many different solutions and there is no transition. I set the class on storyboard but everything is done programmatically. I was wondering if there is any way to switch from the current ViewController to the other from a CollectionView cell.
Picture of storyboard:
This is what it looks like in the simulator:
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")
}
}
Find your UIView
parent UIViewController
using this extension
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
}
}
Add this extension in your project and call like that
if let vc = self.parentViewController as? YourPresentedViewController{
let pushVc = DescriptionViewController()
vc.navigationController?.pushViewController(pushVc, animated: true)
}
note :- UITableView
added in YourPresentedViewController
, so My tableview parentViewController is YourPresentedViewController.