Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)    
        ...
    }
    ...
}
like image 713
CryletCoding Avatar asked Jan 27 '19 12:01

CryletCoding


2 Answers

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 !!
    }
}
like image 120
Asperi Avatar answered Oct 10 '22 13:10

Asperi


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

}
like image 37
Sh_Khan Avatar answered Oct 10 '22 14:10

Sh_Khan