在上图中,我正在尝试创建一个可以搜索特定业务的Yelp API的功能。该子视图视图控制器内部有一个表视图。
我使用以下代码将子视图控制器添加到其父级:
func addChildBusinessSearchController(){
businessSearchResultController = storyboard?.instantiateViewController(withIdentifier: "BusinessSearchResultController") as! BusinessSearchResultController
businessSearchResultController.delegate = self
businessSearchResultController.managedObjectContext = managedObjectContext
view.addSubview(businessSearchResultController.view)
self.addChildViewController(businessSearchResultController)
businessSearchResultController.didMove(toParentViewController: self)
businessSearchResultController.view.translatesAutoresizingMaskIntoConstraints = false
let heightConstraint = businessSearchResultController.view.heightAnchor.constraint(equalToConstant: self.view.bounds.height)
let leadingConstraint = businessSearchResultController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
let widthConstraint = businessSearchResultController.view.widthAnchor.constraint(equalToConstant: self.view.bounds.width)
let topViewUpperConstraint = businessSearchResultController.view.topAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor, constant: -55)
topViewUpperConstraint.identifier = ChildControllersUpperConstraints.businessSearchResultControllerTopConstraintIdentifier
NSLayoutConstraint.activate([heightConstraint, leadingConstraint, widthConstraint, topViewUpperConstraint])
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(businessSearchResultControllerdidPan(_:)))
businessSearchResultController.view.addGestureRecognizer(panGestureRecognizer)
}
在子控制器中,我尝试约束表视图,以便在屏幕上显示正确的尺寸:
func setUpTableView(){
tableView.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = tableView.topAnchor.constraint(equalTo: headerView.bottomAnchor)
let leadingConstraint = tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
let trailingConstraint = tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
let heightConstraint = tableView.heightAnchor.constraint(equalToConstant: 450)
NSLayoutConstraint.activate([topConstraint, leadingConstraint, trailingConstraint, heightConstraint])
tableView.clipsToBounds = true
tableView.backgroundColor = UIColor.clear
}
但是,我有一个问题:我似乎无法将“BusinessSearchResultController”中表格视图的底部约束到标签栏的顶部,以便填充用于此表格视图的所有空间。我理解这是因为我在下面的代码中给它一个非动态高度450:
let heightConstraint = tableView.heightAnchor.constraint(equalToConstant: 450)
但我尝试了不同的方法来获得此表格视图的正确高度,包括将其bottomAnchor
约束到其父topAnchor
的{{1}},这当然会导致错误,因为它们会没有共同的祖先。
非常感谢任何解决此问题的帮助......
答案 0 :(得分:0)
您的表格视图是BusinessSearchResultController
。
您不希望在表格视图中使用高度约束,您希望将表格视图的底部约束到其超级视图的底部 - 就像设置其视图一样领先和尾随约束。
然后,将businessSearchResultController.view
的底部限制在其超级视图的底部,就像设置其前导和尾随约束一样。