0

我只想在代码中实现这种转换。
gif 链接
但我似乎没有使用 Material 的 navigationBarViewController 中的 navigationController。

那么pushViewController方法不能用于页面转换吗?
此代码是使用材料

  import UIKit
  import Material

  /// MaterialCollectionViewDelegate methods.
  extension FeedViewController: MaterialCollectionViewDelegate {
      /// Executed when an item is selected.
      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
          print(indexPath)
          // --------------------------------My Question--------------------------------------//
          let vc = DetailViewController()
          self.navigationBarViewController?.pushViewController(vc, animated: true)
          // ----------------------------------------------------------------------//
      }  
  }

  class FeedViewController: UIViewController {
      private var collectionView: MaterialCollectionView = MaterialCollectionView()

      override func viewDidLoad() {
          super.viewDidLoad()
          prepareView()
          prepareCollectionView()
      }

      override func viewWillLayoutSubviews() {
          super.viewWillLayoutSubviews()
          collectionView.frame = view.bounds
          collectionView.reloadData()
      }

      override func viewWillAppear(animated: Bool) {
          super.viewWillAppear(animated)
          navigationBarViewController?.navigationBarView.titleLabel?.text = "Home"
      }

      /// Prepares view.
      private func prepareView() {
          view.backgroundColor = MaterialColor.grey.lighten4
      }

      /// Prepares the collectionView
      private func prepareCollectionView() {
          collectionView.dataSource = self
          collectionView.delegate = self
          collectionView.spacingPreset = .Spacing1
          collectionView.contentInsetPreset = .Square1
          collectionView.registerClass(MaterialCollectionViewCell.self, forCellWithReuseIdentifier: "MaterialCollectionViewCell")

          // To avoid being hidden under the hovering MenuView.
          view.addSubview(collectionView)
      }
  }

  extension FeedViewController: MaterialCollectionViewDataSource {
      /// Retrieves the items for the collectionView.
      func items() -> Array<MaterialDataSourceItem> {
          return [
              MaterialDataSourceItem(
                  data: [
                      "title": "Summer BBQ",
                      "detail": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                      "date": "February 26, 2016"
                  ],
                  width: 150, // Applied when scrollDirection is .Horizontal
                  height: 150 // Applied when scrollDirection is .Vertical
              ),
              MaterialDataSourceItem(
                  data: [
                      "title": "Birthday gift",
                      "detail": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                      "date": "February 26, 2016"
                  ],
                  width: 250, // Applied when scrollDirection is .Horizontal
                  height: 150 // Applied when scrollDirection is .Vertical
              )
          ]
      }

      /// Number of sections.
      func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
          return 1
      }

      /// Number of cells in each section.
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return items().count
      }

      /// Retrieves a UICollectionViewCell.
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
          let c: MaterialCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MaterialCollectionViewCell", forIndexPath: indexPath) as! MaterialCollectionViewCell

          let item: MaterialDataSourceItem = items()[indexPath.item]

          if let data: Dictionary<String, AnyObject> =  item.data as? Dictionary<String, AnyObject> {

              var cardView: CardView? = c.contentView.subviews.first as? CardView

              // Only build the template if the CardView doesn't exist.
              if nil == cardView {
                  cardView = CardView()

                  c.backgroundColor = nil
                  c.pulseColor = nil
                  c.contentView.addSubview(cardView!)

                  cardView!.pulseScale = false
                  cardView!.divider = false
                  cardView!.depth = .None

                  let titleLabel: UILabel = UILabel()
                  titleLabel.textColor = MaterialColor.grey.darken4
                  titleLabel.font = RobotoFont.regularWithSize(18)
                  titleLabel.text = data["title"] as? String
                  cardView!.titleLabel = titleLabel

                  let detailLabel: UILabel = UILabel()
                  detailLabel.numberOfLines = 2
                  detailLabel.textColor = MaterialColor.grey.darken2
                  detailLabel.font = RobotoFont.regular
                  detailLabel.text = data["detail"] as? String
                  cardView!.detailView = detailLabel

                  c.contentView.addSubview(cardView!)
              } else {
                  cardView?.titleLabel?.text = data["title"] as? String
                  (cardView?.detailView as? UILabel)?.text = data["detail"] as? String
              }

              cardView!.frame = c.bounds
          }

          return c
      }
  }
4

2 回答 2

0

您需要将初始视图控制器嵌入到导航控制器中。

然后你可以使用,

let newVC = self.storyboard?.instantiateViewControllerWithIdentifier("DetailViewControllerIdentifier") as! DetailViewController

self.navigationController?.pushViewController(newVC, animated: true)

根据您作为示例提供的 gif,转换是从 tableview 发生的。uitableview因此,您可以在委托方法内执行转换didSelectItemAtIndexPath

希望有帮助!

于 2016-03-04T08:05:15.630 回答
0

在 Material 1.36.0中,有两个新类 NavigationBar 和 NavigationController。NavigationController 是 UINavigationController 的子类,因此在项目堆栈上推送和弹出 UIViewController 的所有功能都是相同的,同时能够轻松自定义控制器的外观和感觉。

示例 App 项目展示了如何使用它。

于 2016-03-11T08:04:57.683 回答