[英]Resize UIView loaded from xib as tableHeaderView
我有一個以編程方式創建的UITableViewController
(沒有xib)。 我正在嘗試使用從具有自動布局約束的xib加載的UIView
,並將其用作tableHeaderView
(而不是節頭)。 然后,我需要根據將包含動態文本的UILabel
的縮放來自動調整UIView
的大小。
這就是我創建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
這是你的答案。 從上面刪除下面的代碼,一切都將正常工作。
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
請享用 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.