是否可以像管理子视图一样管理childViewController框架?我的意思是,像基于插入文本的UILabel一样,childViewController可以根据内容计算框架吗?
我使用intrinsicContentSize来确定自定义UIView的contentSize。
我找到了preferredContentSize属性,但当我更改它的值时,什么也没有发生。
示例
我有包含UITableView的FromViewController。UITableView将包含大约2-3个单元格,这些单元格的整体高度将小于屏幕高度。
现在,我想将FromViewController作为childViewContoller添加到另一个ViewController中。我将把parentView的FromViewController视图固定到前导、尾随和底部。
我的目标是:我不想为FromViewController设置高度约束。我想自己用FromViewController计算高度。
发布于 2018-04-10 15:31:47
你可以试试这个。
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])
}
}https://stackoverflow.com/questions/49747802
复制相似问题