如何调整子视图控制器的高度以匹配容器视图的高度

问题描述 投票:0回答:1

我有一个高度可调的容器视图。现在我想根据容器视图的高度设置子视图控制器视图的相同框架。有时它在模拟中有效,但通常会失败。子视图控制器视图的框架通常与开头定义的框架保持一致。你知道解决这个问题的解决方法吗?

这是我的主要ViewControllercontainerView命名为bottomView

class ViewController: UIViewController {
    var startPosition: CGPoint!
    var originalHeight: CGFloat = 0
    var bottomView = UIView()
    var gestureRecognizer = UIPanGestureRecognizer()
    let scrollView = UIScrollView()
    let controller = EditorViewController()

    override func viewDidLoad() {
        self.view.backgroundColor = .white 
        view.addSubview(bottomView)
        bottomView.translatesAutoresizingMaskIntoConstraints = false
        bottomView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        bottomView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        bottomView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        bottomView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        bottomView.backgroundColor = .white

        gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(viewDidDragged(_:)))
        bottomView.isUserInteractionEnabled = true
        bottomView.addGestureRecognizer(gestureRecognizer)

        // add childviewController

        self.addChild(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        controller.view.frame = bottomView.bounds
        bottomView.addSubview(controller.view)        
        controller.view.rightAnchor.constraint(equalTo: bottomView.rightAnchor).isActive = true
        controller.view.leftAnchor.constraint(equalTo: bottomView.leftAnchor).isActive = true
        controller.view.bottomAnchor.constraint(equalTo: bottomView.bottomAnchor).isActive = true
        controller.view.topAnchor.constraint(equalTo: bottomView.topAnchor).isActive = true
        controller.didMove(toParent: self)

    }
}

childView控制器看起来像这样:

class EditorViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .red   
    }

并以这种方式我改变容器视图的高度,并尝试调整儿童viewController的高度。但它不起作用。

@objc func viewDidDragged(_ sender: UIPanGestureRecognizer) {

        if sender.state == .began {
            startPosition = gestureRecognizer.location(in: self.view) // the postion at which PanGestue Started
            originalHeight = bottomView.frame.size.height
        }
        if sender.state == .began || sender.state == .changed {
            let endPosition = sender.location(in: self.view)
            let difference = endPosition.y  - startPosition.y
            let newHeight = originalHeight - difference
            if self.view.frame.size.height - endPosition.y < 100 {
                bottomView.frame = CGRect(x: 0, y: self.view.frame.size.height - 100, width: self.view.frame.size.width, height: 100)
                controller.view.frame = bottomView.bounds  
            } else {
                bottomView.frame = CGRect(x: 0, y: self.view.frame.size.height - newHeight, width: self.view.frame.size.width, height: newHeight)
                controller.view.frame = bottomView.bounds
            }
        }

        if sender.state == .ended || sender.state == .cancelled {
            //Do Something
        }
    }
swift dynamic height uicontainerview childviewcontroller
1个回答
0
投票

尝试使用约束来更改高度。就像是:

class ViewController: UIViewController {
  ...

  var heightConstraint: NSLayoutConstraint?

  override func viewDidLoad() {

    ...

    heightConstraint = bottomView.heightAnchor.constraint(equalToConstant: 100)
    heightConstraint?.isActive = true

    ....

  }

  @objc func viewDidDragged(_ sender: UIPanGestureRecognizer) {

    ...

    if sender.state == .began || sender.state == .changed {
      let endPosition = sender.location(in: self.view)
      let difference = endPosition.y  - startPosition.y
      let newHeight = originalHeight - difference
      if self.view.frame.size.height - endPosition.y < 100 {
        heightConstraint?.constant = 100
      } else {
        heightConstraint?.constant = newHeight
      }
    }

    ...

  }
}
© www.soinside.com 2019 - 2024. All rights reserved.