简体   繁体   English

如何将 FlutterViewController 添加到 UIViewController

[英]How to add a FlutterViewController to a UIViewController

All the documentation that I can find for Flutter's Add-to-App demonstrates how to push/segue/present a newly created FlutterViewController.我可以为 Flutter 的 Add-to-App 找到的所有文档都演示了如何推送/segue/呈现新创建的 FlutterViewController。 I need to add a flutter view to a tab within a tabbar.我需要将 flutter 视图添加到选项卡栏中的选项卡。 I tried changing my class's super class from UIViewController to FlutterViewController which worked, but is not using the flutter engine that is created within the app delegate.我尝试将我班级的超级 class 从UIViewControllerFlutterViewController ,但没有使用在应用程序委托中创建的 flutter 引擎。 This solution resulted in showing in the launch screen before the flutter view, which isn't desired.此解决方案导致在 flutter 视图之前显示在启动屏幕中,这是不希望的。

How do I add a flutter view to a UIViewController ?如何将 flutter 视图添加到UIViewController Or how do I assign an engine to a FlutterViewController from within the class?或者如何从 class 中将引擎分配给FlutterViewController

I found a solution which adds the flutter view to the view controller.我找到了一个解决方案,它将 flutter 视图添加到视图 controller 中。

// create an extension for all UIViewControllers
extension UIViewController {
     /**
         Add a flutter sub view to the UIViewController
         sets constraints to edge to edge, covering all components on the screen
     */
    func addFlutterView(with engine: FlutterEngine) {
        // create the flutter view controller
        let flutterViewController = FlutterViewController(engine: engine, nibName: nil, bundle: nil)
        
        addChild(flutterViewController)
        
        guard let flutterView = flutterViewController.view else { return }

        // allows constraint manipulation
        flutterView.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(flutterView)
        
        // set the constraints (edge-to-edge) to the flutter view
        let constraints = [
            flutterView.topAnchor.constraint(equalTo: view.topAnchor),
            flutterView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            flutterView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            flutterView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ]

        // apply (activate) the constraints
        NSLayoutConstraint.activate(constraints)

        flutterViewController.didMove(toParent: self)
        
        // updates the view with configured layout
        flutterView.layoutIfNeeded()
    }
}

with any UIViewController, you can add a flutter subview now.对于任何 UIViewController,您现在可以添加 flutter 子视图。

override func viewDidLoad() {
    super.viewDidLoad()
    
    // get the flutter engine for the view
    let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
    
    // add flutter view
    addFlutterView(with: flutterEngine)

    // Do any additional setup after loading the view.
}

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

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