nwales
nwales

Reputation: 3561

TableHeaderView with UITextView not sized correctly by its UITableViewController when UITextView.isScrollEnabled = false

I have a tableView with a tableHeaderView that is assigned through a nib file. This tableHeaderView contains a TextView (orange background) that is sized with a height constraint using sizeThatFits.

let textViewSize = titleView.sizeThatFits(CGSize(width: titleView.frame.size.width, height: CGFloat(MAXFLOAT)))
    textViewHeightConstraint.constant = textViewSize.height

The constraints of this header view are setup to properly drive it’s height.

enter image description here

When the tableView sizes the frame for the tableHeaderView i am getting very strange results from the headerView.systemLayoutSizeFitting when scrolling in the UITextView is no enabled.

if let headerView = tableView.tableHeaderView {
    let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
    var frame = headerView.frame
    frame.size.height = height

    if headerView.frame.height != height {
       headerView.frame = frame
       tableView.tableHeaderView = headerView
       headerView.setNeedsLayout()
       headerView.layoutIfNeeded()
    }}

If scrolling in the textView is enabled the tableHeaderView scales as expected, but the textView is cutting off the top line.

enter image description here

If scrolling for the textView is disabled (desired setting) then the height for this headerView is calculated to be larger than the size required. The length of text seems to multiply this effect.

enter image description here

I should also add that the TextView is being assigned attributed text with various paragraph and line spacing options.

Any suggestions as to what could be going on are much appreciated.

Upvotes: 0

Views: 374

Answers (1)

nwales
nwales

Reputation: 3561

I have resolved my issue. I am still not sure why the above problems were occurring, but when I applied the header view in a storyboard (vs loading it in via a nib file and assigning it to tableView.tableHeaderView at runtime) all of my autolayout issues disappeared.

Still would like to know why it was mis-behaving and if there is a way to get my table header views to work through a xib files

Upvotes: 0

Related Questions