简体   繁体   中英

Bottom Navigation Drawer doesn't show up (Swift)

I currently working on an iOS app and I want to use a bottom navigation drawer from material-io. So I did it like it is explained in the examples on the site . But when I present the navigation Drawer the ViewController only gets a bit darker and the contentView of the drawer isn't shown.

Here is my Code:

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)    
        ...
    }
    ...
}

Your view controller to be shown in drawer must have specified preferred content size.

Here is a demo of minimal controller. ( Note: modalPresentationStyle = .popover has no effect on MDCBottomDrawerViewController )

Tested with Xcode 12

演示

  // 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 !!
    }
}

Move this to viewWillAppear / viewDidAppear once as it's too early for viewDidLoad to present a 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
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM