我有一个UITableViewController
我以编程方式创建(没有xib)。我正在尝试使用从具有自动布局约束的xib加载的UIView
,并将其用作我的tableHeaderView
(不是节标题)。然后,我需要根据其中包含动态文本的UIView
的缩放来自动调整UILabel
。
listing of all the drivers for USB Samsung
这是我创建UIView
:
class HeaderTableLargeHeaderView: UIView {
// Our title label with autolayout constraints
@IBOutlet weak var titleLabel: UILabel!
// Convience method to load for xib
class func instanceFromNib() -> UIView {
return UINib(nibName: "HeaderTableLargeHeaderView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel.preferredMaxLayoutWidth = titleLabel.bounds.width
}
}
然后我尝试在UITableViewController
:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let header = HeaderTableLargeHeaderView.instanceFromNib()
header.frame = CGRectMake(0, 0, self.view.frame.size.width, 100)
let height = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = header.frame
frame.size.height = height
header.frame = frame
header.setNeedsLayout()
header.layoutIfNeeded()
tableView.tableHeaderView = header
}
但是我收到了这些错误:
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:0x7c061050 V:[UILabel:0x7c0b77e0'Label']-(8)-| (Names: '|':Care_Com.HeaderTableLargeHeaderView:0x7c04a550 )>",
"<NSLayoutConstraint:0x7c0c6590 V:|-(8)-[UILabel:0x7c0b77e0'Label'] (Names: '|':Care_Com.HeaderTableLargeHeaderView:0x7c04a550 )>",
"<NSLayoutConstraint:0x797576d0 'UIView-Encapsulated-Layout-Height' V:[Care_Com.HeaderTableLargeHeaderView:0x7c04a550(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7c061050 V:[UILabel:0x7c0b77e0'Label']-(8)-| (Names: '|':Care_Com.HeaderTableLargeHeaderView:0x7c04a550 )>
我认为我理解但不确定如何解决。我认为它告诉我的是它无法正确设置UILabel
的约束,因为它不知道UIView
的界限......我想。
如果我这样做,我没有得到任何错误,它加载正常,但高度是静态的100
。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let header = HeaderTableLargeHeaderView.instanceFromNib()
header.frame = CGRectMake(0, 0, self.view.frame.size.width, 100)
header.setNeedsLayout()
header.layoutIfNeeded()
tableView.tableHeaderView = header
}
我已经尝试过设置这些但仍无效:(
self.view.translatesAutoresizingMaskIntoConstraints = false
self.tableView.translatesAutoresizingMaskIntoConstraints = false
我也尝试过这样做:
header.translatesAutoresizingMaskIntoConstraints = false
答案 0 :(得分:1)
这是你的答案。从上面删除下面的一堆代码,一切都会正常工作。
header.translatesAutoresizingMaskIntoConstraints = false
header.frame = CGRectMake(0, 0, self.view.frame.size.width, 0)
header.setNeedsLayout()
header.layoutIfNeeded()
let height = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = header.frame
frame.size.height = height
header.frame = frame
和
titleLabel.preferredMaxLayoutWidth = titleLabel.bounds.width
那些是不必要的。更新了以下网址中的存储库。
https://github.com/mahesh-agrawal/HeaderTestStack
享受:)