如何在iOS项目的viewController中向flutterViewController添加边距/填充

时间:2019-09-23 10:21:02

标签: ios xcode flutter

我将flutter添加到现有的Android和IOS项目中,这做得很完美,但是如何在IOS项目的viewController中向flutterView中添加顶部填充/边距,以便出现顶部导航栏,因为flutterView覆盖顶部导航栏,在这里是快速视图控制器代码:

import UIKit
import Flutter

class FlutterView: UIViewController {

   override func viewDidLoad() {
      super.viewDidLoad()

      let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine;
      let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!;
      self.present(flutterViewController, animated: false, completion: nil)

      flutterViewController.setInitialRoute("referral")

   }

}

1 个答案:

答案 0 :(得分:1)

我希望这对您有帮助

class FlutterView: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine;
        let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!;
//        self.present(flutterViewController, animated: false, completion: nil)
        addChild(flutterViewController)


        flutterViewController.setInitialRoute("referral")
    }


    func addChildViewControllerWithPadding(_ childController: UIViewController, padding: CGFloat) {
        addChild(childController)
        view.addSubview(childController.view)

        constraintsWithPaddingBetween(holderView: view, topView: childController.view, padding: padding)
        childController.didMove(toParent: self)
    }

    func constraintsWithPaddingBetween(holderView: UIView, topView: UIView, padding: CGFloat) {
        guard holderView.contains(topView) else {
            return
        }
        topView.translatesAutoresizingMaskIntoConstraints = false
        let pinTop = NSLayoutConstraint(item: topView, attribute: .top, relatedBy: .equal,
                                        toItem: holderView, attribute: .top, multiplier: 1.0, constant: padding)
        let pinBottom = NSLayoutConstraint(item: topView, attribute: .bottom, relatedBy: .equal,
                                           toItem: holderView, attribute: .bottom, multiplier: 1.0, constant: padding)
        let pinLeft = NSLayoutConstraint(item: topView, attribute: .left, relatedBy: .equal,
                                         toItem: holderView, attribute: .left, multiplier: 1.0, constant: padding)
        let pinRight = NSLayoutConstraint(item: topView, attribute: .right, relatedBy: .equal,
                                          toItem: holderView, attribute: .right, multiplier: 1.0, constant: padding)
        holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
    }

}

在上面的示例中,您将在当前控制器内使用填充,但是由于您会将其添加为子代,因此它将自动成为您控制器的一部分,我想它位于当前导航栏下方。

也许最好在控制器中创建一个专用的子视图。

另外,我建议您使用

addChildViewControllerWithPaddingconstraintsWithPaddingBetween

作为UIViewControllerUIView的扩展名。