iosswiftuitableviewtableviewuicontrol

UITableViewCell desn't update it's heigh to fit custom UIControl height


I've created a custom slider inherited from UIControl and pass it into UITableViewCell.

The problem is that when the content of my custom slider is bigger than UITableViewCell's height, the cell won't update it's height to fit slider's content height

Here is my hierarchy of TableViewCell

enter image description here

This is how it looks like(the background blue view is a cell). All views I created in code in custom slider control:

enter image description here

This is how it looks like on the device:

enter image description here

As you can see I have a larger content of my custom control, but the cell doesn't want to update its constraints to fit control's content. One more thing, as you can see on the first screenshot, I have height constraint >= 60, but if I remove this constraint, than my custom slider won't be visible at all:

enter image description here

Here is my code of custom slider, but I guess I messed something of updating constraints of parent view(UITableViewCell) or calculating setting parentView's(UITableViewCell) content height:

private func updateValueLabelFrame() {
    func positionForValueInTrack() -> CGFloat {
        return trackView.frame.width * ((value - minimumValue) / (maximumValue - minimumValue))
    }
    
    func originForValueInTrack(height: CGFloat) -> CGPoint {
        let x = positionForValueInTrack() - height / 2
        return CGPoint(x: x, y: (trackView.frame.height - height) / 2)
    }
    trackView.setNeedsDisplay()
    valueLabel.sizeToFit()
    let biggerSize = valueLabel.frame.width > valueLabel.frame.height ? valueLabel.frame.width : valueLabel.frame.height
    var valueViewHeight = self.frame.height - (self.frame.height * 0.3)
    
    if biggerSize > valueViewHeight {
        valueViewHeight = biggerSize + 20
        self.frame.size = CGSize(width: self.frame.width, height: valueViewHeight / 0.7)
        self.setNeedsLayout()
    }
    let valueViewSize = CGSize(width: valueViewHeight, height: valueViewHeight)
    self.valueView.frame = CGRect(origin: originForValueInTrack(height: valueViewHeight), size: valueViewSize)
    
    let valueLabelSize = valueLabel.sizeThatFits(valueViewSize)
    let valueLabelOrigin = CGPoint(x: valueView.bounds.midX - valueLabelSize.width / 2, y: valueView.bounds.midY - valueLabelSize.height / 2)
    valueLabel.frame = CGRect(origin: valueLabelOrigin, size: valueLabelSize)
    
    valueView.layer.cornerRadius = valueView.frame.size.width / 2
}

private func updateFrame() {
    minimumLabel.sizeToFit()
    maximumLabel.sizeToFit()
    minimumLabel.frame.origin = CGPoint(x: self.bounds.minX + minMaxLabelsOffsetToBorder, y: self.bounds.midY - minimumLabel.frame.height / 2)
    maximumLabel.frame.origin = CGPoint(x: self.bounds.maxX - (maximumLabel.frame.width + minMaxLabelsOffsetToBorder), y: self.bounds.midY - maximumLabel.frame.height / 2)
    let trackLayerSize = CGSize(width: (maximumLabel.frame.minX - trackLayerOffsetFromLabels) - (minimumLabel.frame.maxX + trackLayerOffsetFromLabels), height: self.bounds.height / 3)
    let trackLayerOrigin = CGPoint(x: minimumLabel.frame.maxX + trackLayerOffsetFromLabels, y: self.bounds.midY - trackLayerSize.height / 2)
    trackView.frame = CGRect(origin: trackLayerOrigin, size: trackLayerSize)
    createReplicatorLayer()
    updateValueLabelFrame()
}

Solution

  • As per OP...

    You're missing constraints that are needed to auto-size the height of the cell.