簡體   English   中英

調整從Xib加載的UIView的大小為tableHeaderView

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM