Flutter与iOS混合开发实战
import UIKit
import Flutter
class HybridViewController: UIViewController {
private var flutterEngine: FlutterEngine!
override func viewDidLoad() {
super.viewDidLoad()
// 初始化Flutter引擎
flutterEngine = FlutterEngine(name: "myFlutterEngine")
// 启动引擎
startFlutterEngine()
}
private func startFlutterEngine() {
flutterEngine.run();
// 创建FlutterViewController并设置初始路由及引擎
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
// 设置FlutterViewController的视图为子视图
addChild(flutterViewController)
view.addSubview(flutterViewController.view)
flutterViewController.didMove(toParent: self)
// 自适应Flutter视图尺寸
flutterViewController.view.frame = view.bounds
flutterViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
这段代码展示了如何在iOS原生应用中嵌入Flutter视图。首先,在viewDidLoad
方法中,我们初始化了一个Flutter引擎,并在适当的时候启动它。然后,我们创建了一个FlutterViewController
实例,并将其视图作为子视图添加到当前视图控制器的视图中。最后,我们设置了Flutter视图的自适应尺寸。这样,Flutter就可以在iOS原生应用中提供其富文本和交互体验。
评论已关闭