Last active
July 18, 2023 00:42
-
-
Save yaizudamashii/90ecad6bf0f0146f038d714e1f36adf2 to your computer and use it in GitHub Desktop.
UIViewController extension to support Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Flutter | |
extension UIViewController { | |
// 全てのUIViewControllerでフラターを内包できるように関数を定義します。 | |
private func createFlutterViewController() -> FlutterViewController? { | |
if let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.informationTabFlutterEngine { | |
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil) | |
return flutterViewController | |
} | |
return nil | |
} | |
// フラターのUIが何かのサブビューに入るべき場合はこの関数を使います | |
private func constrainFlutterView(_ flutterView: UIView, containingView: UIView) { | |
flutterView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint(item: flutterView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: containingView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0).isActive = true | |
NSLayoutConstraint(item: flutterView, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: containingView, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: 0).isActive = true | |
NSLayoutConstraint(item: flutterView, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: containingView, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1, constant: 0).isActive = true | |
NSLayoutConstraint(item: flutterView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: containingView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0).isActive = true | |
} | |
// フラター画面を作成する時にこの関数を使います。 | |
func setupFlutterView(flutterEngine: FlutterEngine, | |
flutterRoute: String?, | |
containingView: UIView, | |
title: String?) -> FlutterViewController { | |
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil) | |
flutterViewController.title = title | |
if let flutterRoute: String = flutterRoute { | |
flutterViewController.pushRoute(flutterRoute) | |
} | |
flutterViewController.view.frame = containingView.bounds | |
containingView.addSubview(flutterViewController.view) | |
if containingView != self.view { | |
self.constrainFlutterView(flutterViewController.view, containingView: containingView) | |
} | |
self.addChild(flutterViewController) | |
flutterViewController.didMove(toParent: self) | |
return flutterViewController | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment