底部导航抽屉不显示(快速)

时间:2019-01-27 12:04:22

标签: ios swift material-design

我目前正在使用iOS应用,我想使用material-io的底部导航抽屉。因此,我做到了,就像在site上的示例中所解释的那样。但是,当我显示导航抽屉时,ViewController只会变暗一点,而抽屉的contentView不会显示。

这是我的代码:

import Foundation
import UIKit
import MaterialComponents

class CreateSubjectView: UIViewController, UITextFieldDelegate {
    ...
    override func viewDidLoad() {
        ...
        let bottomDrawerViewController = MDCBottomDrawerViewController()
        self.modalPresentationStyle = .popover
        let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "TEST")
        bottomDrawerViewController.contentViewController = newViewController

        present(bottomDrawerViewController, animated: true, completion: nil)    
        ...
    }
    ...
}

2 个答案:

答案 0 :(得分:3)

要在抽屉中显示的视图控制器必须指定了首选的内容大小。

这里是最小控制器的演示。 (注意:modalPresentationStyle = .popoverMDCBottomDrawerViewController 没有影响)

经过Xcode 12测试

demo

  // button action in parent controller
  @objc private func presentNavigationDrawer() {
    let bottomDrawerViewController = MDCBottomDrawerViewController()
    bottomDrawerViewController.contentViewController = DemoViewController()
    present(bottomDrawerViewController, animated: true, completion: nil)
  }
}

class DemoViewController: UIViewController {

    override func loadView() {
        super.loadView()
        let view = UIView()
        view.backgroundColor = .red
        self.view = view
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        // specify your content preferred height explicitly
        self.preferredContentSize = CGSize(width: 0, height: 400)     // required !!
    }

    @available(iOS 11.0, *)
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()

        // specify your content preferred height explicitly
        self.preferredContentSize = CGSize(width: 0, height: 400)     // required !!
    }
}

答案 1 :(得分:0)

将其一次移至viewWillAppear / viewDidAppear,因为viewDidLoad提交vc还为时过早

class CreateSubjectView: UIViewController, UITextFieldDelegate {

    let bottomDrawerViewController = MDCBottomDrawerViewController()
    var once = true
    override func viewDidLoad() {
      super.viewDidLoad()

    }

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

        if once {

            let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "TEST")
            bottomDrawerViewController.contentViewController = newViewController
            present(bottomDrawerViewController, animated: true, completion: nil)

            once  = false
        }
    }

}