mrg*_*t96 2 uiviewcontroller ios swift flutter flutter-add-to-app
我能找到的 Flutter 的 Add-to-App 的所有文档都演示了如何推送/继续/呈现新创建的 FlutterViewController。我需要将颤动视图添加到选项卡栏中的选项卡。我尝试将班级的超类更改为UIViewController有效FlutterViewController,但没有使用应用程序委托中创建的颤动引擎。此解决方案导致在启动屏幕中显示颤动视图之前,这是不希望的。
如何向 中添加颤动视图UIViewController?或者如何将引擎分配给FlutterViewController类内的引擎?
我找到了一个将颤动视图添加到视图控制器的解决方案。
// 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()
}
}
Run Code Online (Sandbox Code Playgroud)
对于任何 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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2834 次 |
| 最近记录: |