7

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)    
        ...
    }
    ...
}
2
  • try to move your code to viewDidAppear. Commented Jan 27, 2019 at 12:47
  • this problem occurs even if the drawer is activated with a button well after viewDidAppear. Commented Jun 25, 2020 at 7:02

2 Answers 2

3

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

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 !!
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Chamitha - see solution
@kristofferarl - see solution
@jeffrey-liu - see solution
Thank you. This is the only example solution I could find on the entire web. Do you know how to get the drawer to slide in from the side instead of the bottom, like it appears to in their documentation (material.io/components/navigation-drawer)? Or is this just a bottom-up component?
1

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

}

3 Comments

See something flashing to the right, but nothing really shows.
Any update on this? Struggling with the same issue.
Anyone resolve this? I'm presenting the drawer on a button click therefore it is after viewDidLoad and viewWillAppear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.