为什么 Child Table View 的大小不正确?
Why is Child Table View not sizing properly?
首先,请不要提出建议我删除 TableViewController 作为子视图的“聪明”解决方案。谢谢。
总结
我正在以编程方式添加一个 Tableviewcontroller,作为一个固定大小为 216 的视图的子视图。我一直在处理约束....并且使用 View Hierachy Debugger,我看到 TableView 总是有一个高度852...这基本上是屏幕的全尺寸。我怎样才能正确调整 TableView 的大小以适应它的包含视图?
enter image description here
下面是我试图用来约束事物的一堆代码……无济于事。谢谢。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var xyz: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let child = UITableViewController()
xyz.addSubview(child.view)
self.addChild(child)
child.didMove(toParent: self)
//child.view.translatesAutoresizingMaskIntoConstraints = false
let safeArea = xyz.layoutMarginsGuide
var height = child.view.heightAnchor.constraint(equalToConstant: 292)
height = height.constraintWithMultiplier(2000)
height.isActive = true
child.view.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
child.view.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
child.view.leftAnchor.constraint(equalTo: safeArea.leftAnchor).isActive = true
child.view.rightAnchor.constraint(equalTo: safeArea.rightAnchor).isActive = true
}
}
extension NSLayoutConstraint {
func constraintWithMultiplier(_ multiplier: CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(item: self.firstItem!, attribute: self.firstAttribute, relatedBy: self.relation, toItem: self.secondItem, attribute: self.secondAttribute, multiplier: multiplier, constant: self.constant)
}
}
取消注释这行代码
child.view.translatesAutoresizingMaskIntoConstraints = false
首先,请不要提出建议我删除 TableViewController 作为子视图的“聪明”解决方案。谢谢。
总结
我正在以编程方式添加一个 Tableviewcontroller,作为一个固定大小为 216 的视图的子视图。我一直在处理约束....并且使用 View Hierachy Debugger,我看到 TableView 总是有一个高度852...这基本上是屏幕的全尺寸。我怎样才能正确调整 TableView 的大小以适应它的包含视图?
enter image description here
下面是我试图用来约束事物的一堆代码……无济于事。谢谢。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var xyz: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let child = UITableViewController()
xyz.addSubview(child.view)
self.addChild(child)
child.didMove(toParent: self)
//child.view.translatesAutoresizingMaskIntoConstraints = false
let safeArea = xyz.layoutMarginsGuide
var height = child.view.heightAnchor.constraint(equalToConstant: 292)
height = height.constraintWithMultiplier(2000)
height.isActive = true
child.view.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
child.view.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
child.view.leftAnchor.constraint(equalTo: safeArea.leftAnchor).isActive = true
child.view.rightAnchor.constraint(equalTo: safeArea.rightAnchor).isActive = true
}
}
extension NSLayoutConstraint {
func constraintWithMultiplier(_ multiplier: CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(item: self.firstItem!, attribute: self.firstAttribute, relatedBy: self.relation, toItem: self.secondItem, attribute: self.secondAttribute, multiplier: multiplier, constant: self.constant)
}
}
取消注释这行代码
child.view.translatesAutoresizingMaskIntoConstraints = false