gpt4 book ai didi

swift - 以编程方式添加约束会导致崩溃

转载 作者:可可西里 更新时间:2023-11-01 01:21:45 25 4
gpt4 key购买 nike

我想在从底部新 TableView 滑动时以编程方式添加一些约束,但到目前为止我无法理解我做错了什么。在我的 super view 中,我有 map view 必须更改 bottom constraint 以便为新的 tableView 腾出空间按钮被点击。

基本上对于我的 TableView ,我需要 4 个约束:

  • top contains to the map bottom
  • 标签栏顶部约束的底部约束
  • 从 super View 引导和跟踪

我总是崩溃:

Impossible to set up layout with view hierarchy unprepared for constraint.

基于以下设置约束:here

代码如下:

@IBAction func test(_ sender: UIButton) {
let tableView = UITableView()
tableView.backgroundColor = UIColor.red
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)

let topConstraint = NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: self.mapView, attribute: .bottom, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: self.tabBarController?.tabBar, attribute: .top, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: tableView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: tableView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)

view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: self.mapView, attribute: .bottom, multiplier: 1, constant: 0))
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: self.tabBarController?.tabBar, attribute: .top, multiplier: 1, constant: 0))
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0))
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0))

self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.5, animations: {
self.mapBottomConstaint.constant = 200
self.centerButtonBottomConstaint.constant = 208
self.view.layoutIfNeeded()
})
}

最佳答案

您正在尝试为 View 层次结构之外的 View 设置约束。自动布局系统提供布局指南,帮助您将 View 与导航栏和标签栏对齐。

在底部约束中,将 self.tabBarController?.tabBar 替换为 bottomLayoutGuide .

作为旁注,新的布局语法(在 iOS 9+ 上可用)使得在没有第三方库的情况下创建编程约束变得更加容易:

let topConstraint = tableView.topAnchor.constraint(equalTo: mapView.bottomAnchor)
let bottomConstraint = tableView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor)
let leadingConstraint = tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
let trailingConstraint = tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)

有关创建编程约束的更多信息 here .

关于swift - 以编程方式添加约束会导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43652367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com