简体   繁体   English

底部导航抽屉不显示 (Swift)

[英]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.我目前正在开发一个 iOS 应用程序,我想使用 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.但是当我展示导航抽屉时,ViewController 只会变暗一点,并且没有显示抽屉的 contentView。

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 ) 注意: modalPresentationStyle = .popoverMDCBottomDrawerViewController没有影响

Tested with Xcode 12使用 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将其移动到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
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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