ORStudios
ORStudios

Reputation: 3233

Incorrect Height When Setting UITextView Height Inside UITableViewCell

I am trying to set the height of a UITextView so that it fits all the text within the view without scrolling. Autolayout seems to be working because it adjusts the size in every instance, the problem is however that it never quite gets the correct height. There always seems to be a chunk missing from the bottom.

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"VideoContentCell";

    VideoContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[VideoContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    /// ----------
    /// Setup the video title
    /// ----------

    [cell.videoTitle setText:[self.videoObject.videoTitle capitalizedString]];


    /// ----------
    /// Setup the channel image
    /// ----------

    [cell.imageChannel.layer setCornerRadius:15.f];

    if(self.videoObject.videoPublisherThumbnail != (id)[NSNull null]){
        [cell.imageChannel sd_setImageWithURL:[NSURL URLWithString:self.videoObject.videoPublisherThumbnail]
                                 placeholderImage:nil];
    }


    /// ----------
    /// Setup the channel title
    /// ----------

    [cell.channelInfo setText:[self.videoObject.videoPublisher capitalizedString]];


    /// ----------
    /// Setup the video description
    /// ----------

    [cell.videoInfo   setText:self.videoObject.videoDescription];

    CGSize sizeThatFitsTextView = [cell.videoInfo sizeThatFits:CGSizeMake(cell.videoInfo.frame.size.width, MAXFLOAT)];


    //Height to be fixed for SubView same as AdHeight
    NSLayoutConstraint *height = [NSLayoutConstraint
                                  constraintWithItem:cell.videoInfo
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                                  attribute:NSLayoutAttributeNotAnAttribute
                                  multiplier:0
                                  constant:sizeThatFitsTextView.height];

    [cell.videoInfo addConstraint:height];

    [cell.videoInfo setNeedsUpdateConstraints];




    return cell;

}

Upvotes: 0

Views: 283

Answers (1)

Rishi Chaurasia
Rishi Chaurasia

Reputation: 528

Try calling layoutIfNeeded method

[cell.videoInfo addConstraint:height];

 [cell.videoInfo setNeedsUpdateConstraints];

[self.view layoutIfNeeded]; // call this method

Upvotes: 1

Related Questions