2017-10-28 186 views
0

我正在开发一个应用程序没有故事板/ xibs,这被证明是一个挑战。我有一个UICollectionView(广告),用户可以点击单元格导航到新的视图控制器,以显示该广告的详细信息。下面是我遇到的错误的gif。导航视图控制器之间没有故事板/ xibs

enter image description here

正如你所看到的,UICollectionView那种粘我点击细胞后周围的人。我在我的AdsViewController子类adDetailsViewController属性,当我点击细胞代码执行:

adDetailsViewController.adTitle = adTitles[indexPath.row] 
navigationController?.pushViewController(adDetailsViewController, animated: true) 

下面是生命周期方法(目前只是viewDidLoadadDetailsViewController

class AdDetailsViewController: UIViewController { 

    var adTitle: String? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let label = UILabel(frame: view.bounds) 
     label.textAlignment = .center 
     label.center = view.center 
     label.text = adTitle 

     view.addSubview(label) 
    } 
} 

为了完整,这里是其余的AdsViewController

class AdsViewController: UIViewController { 

    lazy var collectionView: AdsCollectionView = { 
     let collectionView = AdsCollectionView(frame: view.frame, collectionViewLayout: TiledFlowLayout()) 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.register(AdCollectionViewCell.self, forCellWithReuseIdentifier: "AdCell") 
     view.addSubview(collectionView) 
     return collectionView 
    }() 

    let adDetailsViewController = AdDetailsViewController() 

    let adTitles = ["1", "2", "3", "4", "5", "6"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     collectionView.translatesAutoresizingMaskIntoConstraints = false 
     collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8).isActive = true 
     collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true 
     collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true 
     collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true 

     view.backgroundColor = .white 
    } 
} 

extension AdsViewController: UICollectionViewDataSource { 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return adTitles.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdCell", for: indexPath) as! AdCollectionViewCell 

     cell.textLabel.text = adTitles[indexPath.row] 

     return cell 
    } 
} 

extension AdsViewController: UICollectionViewDelegate { 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     adDetailsViewController.adTitle = adTitles[indexPath.row] 
     navigationController?.pushViewController(adDetailsViewController, animated: true) 
    } 
} 

extension AdsViewController: UICollectionViewDelegateFlowLayout { 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: (self.view.frame.size.width/2) - 10, height: self.view.frame.size.height/4) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
     return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) 
    } 
} 
+0

发生这种情况是因为您正在动画“ViewControllers”的更改。如果你将'animated:'参数设置为'false',那么如果这是你想达到的目标,那么改变将是即时的。 –

+0

我想要点击单元格时发生正常的转换,即新的视图控制器在其上滑动。我不希望它立即发生。 – eirikvaa

+0

请添加UIViewController的生命周期方法adDetailsViewController – Sander

回答

0

您必须分享更多的代码。有没有特殊的设置f.e.?像透明背景一样?你在viewDidLoad中做什么?你覆盖viewWillAppear,viewDidAppear等,并没有调用子类的功能(如super.viewWillAppear(动画))?这是一个非常常见的错误...

+0

我刚刚添加了'AdsViewController'和'AdDetailsViewController'的代码。 – eirikvaa

0

好的,我修好了。看来,如果我将backgroundColorAdDetailsViewController设置为UIColor.white,那么它会按预期转换。

相关问题