ChildViewController + ContentSize + Autolayout

时间:2018-04-10 07:27:04

标签: ios autolayout

是否可以像子视图一样管理childViewController的框架?我的意思是,childViewController是否可以根据内容计算框架,例如UILabel基于插入的文字?

我使用intrinsicContentSize来确定自定义contentSize的{​​{1}}。

我找到了UIView属性,但是当我更改其值时,没有任何事情发生。

实施例

我的preferredContentSize包含FromViewControllerUITableView将包含大约2-3个单元格,这些单元格的整个高度将小于屏幕高度。

现在我想将UITableView添加到另一个FromViewController作为ViewController。我会将childViewContoller的视图固定在FromViewController的前导,尾随和底部。

我的目标:我不想为parentView设置高度限制。我希望FromViewController自己计算高度。

1 个答案:

答案 0 :(得分:0)

你可以试试这个。

 let objYourVC = self.storyboard?.instantiateViewController(withIdentifier: "YourViewcontroller") as! YourViewcontroller;
   configureChildViewController(childController: objYourVC, onView: self.view)


extension UIViewController {
     func removeChildViewController(controller : UIViewController){
        UIView.animate(withDuration: 0.5, delay: 0.1, options: .transitionFlipFromLeft, animations: {
            controller.view.alpha = 0
        }) { (finished) in
            UIView.animate(withDuration: 0.5, delay: 0.1, options: .transitionFlipFromLeft , animations: {
            }) { (finished) in
                 controller.willMove(toParentViewController: nil)
                 controller.view.removeFromSuperview()
                 controller.removeFromParentViewController()
            }
        }
    }
    func configureChildViewController(childController: UIViewController, onView: UIView?) {
        var holderView = self.view
        if let onView = onView {
            holderView = onView
        }
        childController.view.tag = 1;
        holderView?.addSubview(childController.view)
        addChildViewController(childController)
        constrainViewEqual(holderView: holderView!, view: childController.view)
        childController.didMove(toParentViewController: self)
        childController.view.needsUpdateConstraints()
        childController.view.setNeedsDisplay()
        childController.view.alpha = 0
        UIView.animate(withDuration: 0.5, delay: 0.1, options: .transitionCrossDissolve, animations: {
            childController.view.alpha = 1
        }) { (finished) in
            childController.didMove(toParentViewController: self)
        }
    }


    func constrainViewEqual(holderView: UIView, view: UIView) {
        view.translatesAutoresizingMaskIntoConstraints = false
        let pinTop = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal,
                                        toItem: holderView, attribute: .top, multiplier: 1.0, constant: 0)
        let pinBottom = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal,
                                           toItem: holderView, attribute: .bottom, multiplier: 1.0, constant: 0)
        let pinLeft = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal,
                                         toItem: holderView, attribute: .left, multiplier: 1.0, constant: 0)
        let pinRight = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal,
                                          toItem: holderView, attribute: .right, multiplier: 1.0, constant: 0)

        holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
    }
}
相关问题