I am trying to correctly size my UITableViewCell to fit a UILabel and a UITextView such that all text within the label and the textView is visible.
Now, I'm having trouble getting the appropriate size for the textView. Here is my layout code:
if (_prototypeCell == nil) {
_prototypeCell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DDDataDetectingDetailCell class]) owner:nil options:0] lastObject];
}
[_prototypeCell setFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 1)];
[_prototypeCell configureForItem:self.items[indexPath.row]];
[_prototypeCell setNeedsLayout];
[_prototypeCell layoutIfNeeded];
CGSize size = [_prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
Now, I've made sure that my nib has constraints pinning the top of the cell's contentView, down through the labels and finally to the bottom of the cell. Additionally, the cell has constraints connecting along the horizontal dimension too. However, if I print out this size, I get the following:
(width=486.5, height=61.5)
This is odd because if I print out the textView and the cell, I get this:
(lldb) po _prototypeCell
<DDDataDetectingDetailCell: 0x79bd4700; baseClass = UITableViewCell; frame = (0 0; 320 1); autoresize = RM+BM; layer = <CALayer: 0x79bbba90>>
(lldb) po _prototypeCell.detailTextView
<UITextView: 0x7b9ffc00; frame = (15 1; 290 0); text = 'No need to pay -- Just le...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x79bce590>; layer = <CALayer: 0x79bd6910>; contentOffset: {0, 0}; contentSize: {290, 0}>
So the textView appears to be the correct width, but for some reason is not calculating its height according to the constraints.
Here is my layoutSubviews method:
- (void)layoutSubviews {
[super layoutSubviews];
self.mainLabel.preferredMaxLayoutWidth = self.mainLabel.frame.size.width;
[super layoutSubviews];
}
And here is a pic of my constraints:
why am I getting this funky width from -systemLayoutFittingSize:
?
UPDATE
I think I am incorrectly configuring the constraints on the UITextView (which is, of course, a scroll view) since the intrinsicContentSize of the scrollview reports back as (width=456.5, height=32.5)
self.textView.scrollEnabled = NO;
for your UITextView inside your cell class ? – Zhang