我有一个分配给tableHeaderView的自定义视图。我使用snapkit来制作自定义视图布局。我运行了一些错误,我不知道如何解决它。请帮帮我,谢谢。这是我的代码:
在我的ViewController中:
class QuestionDetailController: BaseViewController {
lazy var tableView = DefaultManager.createTableView()
lazy var dataSource: [HomeListItemModel] = []
lazy var hearderView: QuestionDetailHearderView = QuestionDetailHearderView()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
navigationItem.title = "问题详情"
}
func setupTableView() {
view.addSubview(tableView)
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
tableView.setDelegate(self,self)
tableView.tableHeaderView = hearderView
registerCell()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.snp.makeConstraints { (make) in
make.left.right.top.bottom.equalToSuperview()
}
tableView.sizeHeaderToFit()
}
func registerCell() {
tableView.register(LargeImageViewCell.self, forCellReuseIdentifier: LargeImageViewCell.idenfitier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension QuestionDetailController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let large = tableView.dequeueReusableCell(withIdentifier: LargeImageViewCell.idenfitier, for: indexPath) as! LargeImageViewCell
large.titleLabel.text = "hello"
return large
}
}
UITableView的扩展名包含sizeHeaderToFit():
extension UITableView{
func sizeHeaderToFit() {
if let headerView = self.tableHeaderView {
let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var newFrame = headerView.frame
newFrame.size.height = height
headerView.frame = newFrame
headerView.layoutIfNeeded()
}
}
}
我的自定义视图:
class QuestionDetailHearderView: UIView {
lazy var tagView: TagView = TagView()
lazy var questionLabel = UILabel().then {
$0.setLabel(AppTheme.Colors.title, 19.5)
$0.withNumberOfLines(0)
}
lazy var contentLabel = UILabel().then {
$0.setLabel(AppTheme.Colors.content3, 14.5)
}
lazy var threeImageView: ThreeImageView = ThreeImageView()
lazy var answerLabel = UILabel().then {
$0.setLabel(AppTheme.Colors.auxiliary2, 14.5)
}
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupUI() {
addSubview(tagView)
addSubview(questionLabel)
addSubview(contentLabel)
addSubview(threeImageView)
addSubview(answerLabel)
addUIConstraints()
}
func addUIConstraints() {
tagView.snp.makeConstraints { (make) in
make.left.equalToSuperview().offset(AppDefaults.Constants.space13)
make.right.equalToSuperview().offset(-AppDefaults.Constants.space13)
make.top.equalToSuperview().offset(AppDefaults.Constants.space16)
make.height.equalTo(30)
}
tagView.backgroundColor = UIColor.yellow
questionLabel.snp.makeConstraints { (make) in
make.left.right.equalTo(tagView)
make.top.equalTo(tagView.snp.bottom).offset(AppDefaults.Constants.space15)
}
questionLabel.backgroundColor = UIColor.red
contentLabel.snp.makeConstraints { (make) in
make.left.right.equalTo(questionLabel)
make.top.equalTo(questionLabel.snp.bottom).offset(AppDefaults.Constants.space13)
}
contentLabel.backgroundColor = UIColor.cyan
threeImageView.snp.makeConstraints { (make) in
make.left.right.equalTo(contentLabel)
make.top.equalTo(contentLabel.snp.bottom).offset(AppDefaults.Constants.space13)
make.height.equalTo(77)
}
answerLabel.snp.makeConstraints { (make) in
make.left.right.equalTo(threeImageView)
make.top.equalTo(threeImageView.snp.bottom).offset(AppDefaults.Constants.space13)
make.bottom.equalToSuperview().offset(-AppDefaults.Constants.space16)
}
answerLabel.backgroundColor = UIColor.cyan
// updateData()
}
}
以下是错误的一部分:
[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.
(<SnapKit.LayoutConstraint:0x1740b5ae0@QuestionDetailHearderView.swift#47 ZhiMa_iOS.TagView:0x103850290.left == ZhiMa_iOS.QuestionDetailHearderView:0x10384fed0.left + 13.0>",
"<SnapKit.LayoutConstraint:0x1740b7e20@QuestionDetailHearderView.swift#48 ZhiMa_iOS.TagView:0x103850290.right == ZhiMa_iOS.QuestionDetailHearderView:0x10384fed0.right - 13.0>",
"<NSLayoutConstraint:0x17009fcc0 'UIView-Encapsulated-Layout-Width' ZhiMa_iOS.QuestionDetailHearderView:0x10384fed0.width == 0 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:0x1740b7e20@
QuestionDetailHearderView.swift#48
ZhiMa_iOS.TagView:0x103850290.right ==
ZhiMa_iOS.QuestionDetailHearderView:0x10384fed0.right - 13.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to
catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView
listed in <UIKit/UIView.h> may also be helpful.