Tun*_*nds 2 ios autolayout swift
所以我正在以编程方式将视图添加到tableviewcell的内容视图中,并使用tableview提供的动态高度,我似乎得到以下自动布局警告,我应该担心,因为布局似乎没有搞砸但是它很烦人有一个控制台充满了这些警告.
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2018-10-09 14:51:46.748606+0100 GenericForms[78197:5088471] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600001005fe0 UIView:0x7fa582d162e0.height == 55 (active)>",
"<NSLayoutConstraint:0x600001018960 V:|-(20)-[UIView:0x7fa582d162e0] (active, names: '|':UITableViewCellContentView:0x7fa582d15800 )>",
"<NSLayoutConstraint:0x60000101c910 UIView:0x7fa582d162e0.bottom == UITableViewCellContentView:0x7fa582d15800.bottom - 20 (active)>",
"<NSLayoutConstraint:0x60000103d950 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa582d15800.height == 95 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600001005fe0 UIView:0x7fa582d162e0.height == 55 (active)>
Run Code Online (Sandbox Code Playgroud)
这是我用来在UITableViewCell内容视图内容中添加视图的代码.
if view == nil {
view = UIView(frame: .zero)
view?.backgroundColor = .green
view?.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(view!)
NSLayoutConstraint.activate([
view!.heightAnchor.constraint(equalToConstant: 55),
view!.topAnchor.constraint(equalTo: contentView.topAnchor, constant: edgeInsets.top),
view!.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -edgeInsets.bottom),
view!.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: edgeInsets.left),
view!.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -edgeInsets.right)
])
}
Run Code Online (Sandbox Code Playgroud)
在我的视图控制器中,这是我用来设置表视图的代码.
class SelfSizedTableView: UITableView {
var maxHeight: CGFloat = UIScreen.main.bounds.size.height
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
self.layoutIfNeeded()
}
override var intrinsicContentSize: CGSize {
let height = min(contentSize.height, maxHeight)
return CGSize(width: contentSize.width, height: height)
}
}
class FormViewController: UIViewController {
private let tableView: SelfSizedTableView = {
let tv = SelfSizedTableView()
tv.isScrollEnabled = false
tv.translatesAutoresizingMaskIntoConstraints = false
tv.tableFooterView = UIView()
tv.register(TestTableViewCell.self, forCellReuseIdentifier: "cellId")
return tv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
tableView.dataSource = self
view.addSubview(tableView)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
centerTableViewContent()
}
private func centerTableViewContent() {
tableView.removeConstraints(tableView.constraints)
if tableView.intrinsicContentSize.height > view.safeAreaLayoutGuide.layoutFrame.size.height {
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
tableView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor),
tableView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor)
])
} else {
NSLayoutConstraint.activate([
tableView.heightAnchor.constraint(equalToConstant: tableView.intrinsicContentSize.height),
tableView.widthAnchor.constraint(equalToConstant: view.frame.width),
tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
tableView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
}
}
extension FormViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! TestTableViewCell
switch indexPath.row {
case 0:
cell.backgroundColor = .red
cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
case 1:
cell.backgroundColor = .yellow
cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 20, left: 10, bottom: 20, right: 10))
default:
cell.backgroundColor = .blue
cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 70, left: 10, bottom: 70, right: 10))
}
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
只是分享:有一个很棒的在线工具,可以帮助理解记录的错误消息.只需复制冲突约束列表(包括周围的括号,见下文)并将其粘贴到工具的文本区域.然后它会为您提供一个很好的约束可视化,通常有助于理解错误.试试看!
复制并粘贴此内容:
(
"<NSLayoutConstraint:0x600001005fe0 UIView:0x7fa582d162e0.height == 55 (active)>",
"<NSLayoutConstraint:0x600001018960 V:|-(20)-[UIView:0x7fa582d162e0] (active, names: '|':UITableViewCellContentView:0x7fa582d15800 )>",
"<NSLayoutConstraint:0x60000101c910 UIView:0x7fa582d162e0.bottom == UITableViewCellContentView:0x7fa582d15800.bottom - 20 (active)>",
"<NSLayoutConstraint:0x60000103d950 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa582d15800.height == 95 (active)>"
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
656 次 |
| 最近记录: |