UIView aspect ratio confuses systemLayoutSizeFittingSize

后端 未结 1 1951
猫巷女王i
猫巷女王i 2020-12-31 17:09

Alright, another UITableViewCell dynamic height problem, but with a little twist. Unfortunately I can\'t jump to iOS 8 only when released, otherwise the problem would be sol

相关标签:
1条回答
  • 2020-12-31 17:39

    I was in the same situation, and solved it to override the systemLayoutSizeFittingSize function in the given cell and replace the aspectRation constraint to a height constraint. In the following example the mediaPlayerAspectRationConstraint is added to the view during the view lifecycle and the mediaPlayerHeightContraint is going to use to determine the right height of the cell (see the code below).

    So, I used the

    CGFloat height = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    

    instead of the [cell.contentView systemLayoutSizeFittingSize:].

    - (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize {
        self.mediaPlayerHeightContraint.constant = CGRectGetHeight(self.experienceMedia.frame);
    
        //Lets replace it with an exact height constraint (workaround).
    
        //To remove the Unable to simultaneously satisfy constraints. exceptions
        [self.contentView layoutIfNeeded];
    
        [self.experienceMedia removeConstraint:self.mediaPlayerAspectRationConstraint];
        [self.experienceMedia addConstraint:self.mediaPlayerHeightContraint];
    
        [self setNeedsUpdateConstraints];
    
        CGSize res = [self.contentView systemLayoutSizeFittingSize:targetSize];
    
        [self.contentView layoutIfNeeded];
    
        [self.experienceMedia removeConstraint:self.mediaPlayerHeightContraint];
        [self.experienceMedia addConstraint:self.mediaPlayerAspectRationConstraint];
    
        [self setNeedsUpdateConstraints];
    
        return res;
    }
    
    0 讨论(0)
提交回复
热议问题