简体   繁体   English

调整从Xib加载的UIView的大小为tableHeaderView

[英]Resize UIView loaded from xib as tableHeaderView

I have a UITableViewController that I create programmatically (no xib for it). 我有一个以编程方式创建的UITableViewController (没有xib)。 I'm trying to use a UIView that is loaded from a xib that has autolayout constraints and use it as my tableHeaderView (not section headers). 我正在尝试使用从具有自动布局约束的xib加载的UIView ,并将其用作tableHeaderView (而不是节头)。 I then need to autoresize the UIView based on the scaling of the UILabel that will have dynamic text in it. 然后,我需要根据将包含动态文本的UILabel的缩放来自动调整UIView的大小。

在此处输入图片说明

This is how I create my 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
    }
}

Then I try to load it like this in my UITableViewController : 然后,我尝试在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
}

However I get these errors: 但是我得到这些错误:

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 )>

Which I think I understand but not sure how to fix. 我认为我了解但不确定如何解决。 What I think it's telling me is that it can't properly set the constraints of the UILabel because it doesn't know the bounds of the UIView ....I think. 我认为这说明我不能正确设置UILabel的约束,因为它不知道UIView的范围。

If I do this I don't get any errors and it loads fine however the height is static as 100 . 如果执行此操作,不会出现任何错误,并且可以正常加载,但高度为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
}

I've tried setting these which still doesn't help :( 我试图设置这些仍然没有帮助:(

    self.view.translatesAutoresizingMaskIntoConstraints      = false
    self.tableView.translatesAutoresizingMaskIntoConstraints = false

I've also tried doing this: 我也尝试这样做:

header.translatesAutoresizingMaskIntoConstraints = false

which gets rid of the errors however the height doesn't adjust: 摆脱了错误,但是高度没有调整: 在此处输入图片说明

Here is your answer. 这是你的答案。 Remove the below bunch of code from the above and everything will just work fine. 从上面删除下面的代码,一切都将正常工作。

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

and

titleLabel.preferredMaxLayoutWidth = titleLabel.bounds.width

those were unnecessary. 这些都是不必要的。 Updated repository in the below url. 更新了以下网址中的存储库。

https://github.com/mahesh-agrawal/HeaderTestStack https://github.com/mahesh-agrawal/HeaderTestStack

Enjoy :) 请享用 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM