swift - 如何调整 subview Controller 的高度以匹配容器 View 的高度

标签 swift dynamic height uicontainerview childviewcontroller

我有一个高度可调的容器 View 。现在我想根据容器 View 的高度设置 subview Controller 的 View 的相同框架。有时它在模拟中确实有效,但通常会失败。 subview Controller View 的框架通常与开始时定义的相同。您知道解决此问题的解决方法吗?

这是我的主ViewController,其中containerView名为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 Controller 如下所示:

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

通过这种方式,我更改了容器 View 的高度,并尝试调整子 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
        }
    }

最佳答案

尝试使用约束来更改高度。像这样的东西:

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
      }
    }

    ...

  }
}

关于swift - 如何调整 subview Controller 的高度以匹配容器 View 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55559064/

相关文章:

jquery - 如何获取 jquery 中 currentTarget 的宽度和高度?

java - 调整 ListView 中列表项的大小

ios - 使用通用协议(protocol)作为接口(interface)

ios - Firebase 访问被拒绝?

ios - Swift:如何在使用 XLPagerTabStrip 中的 subview Controller 后返回到第一个 View Controller

swift - 从 XIB-file-Button 显示 ViewController - Swift

linq - 使用在运行时解析的类型调用 System.Linq.Queryable 方法

javascript - 是否可以将js变量写入纯html?

php - 动态文本输入通过ajax保存到数据库

css 列缩小 100% 像素值