是否可以像子视图一样管理childViewController
的框架?我的意思是,childViewController
是否可以根据内容计算框架,例如UILabel
基于插入的文字?
我使用intrinsicContentSize
来确定自定义contentSize
的{{1}}。
我找到了UIView
属性,但是当我更改其值时,没有任何事情发生。
我的preferredContentSize
包含FromViewController
。 UITableView
将包含大约2-3个单元格,这些单元格的整个高度将小于屏幕高度。
现在我想将UITableView
添加到另一个FromViewController
作为ViewController
。我会将childViewContoller
的视图固定在FromViewController
的前导,尾随和底部。
我的目标:我不想为parentView
设置高度限制。我希望FromViewController
自己计算高度。
答案 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])
}
}