简体   繁体   English

UITableViewCell 不会更新它的高度以适应自定义 UIControl 高度

[英]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 .我创建了一个继承自UIControl的自定义 slider 并将其传递给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问题是当我的自定义 slider 的内容大于 UITableViewCell 的高度时,单元格不会更新它的高度以适应滑块的内容高度

Here is my hierarchy of TableViewCell这是我的 TableViewCell 层次结构

在此处输入图像描述

This is how it looks like(the background blue view is a cell).这就是它的样子(背景蓝色视图是一个单元格)。 All views I created in code in custom slider control:我在自定义 slider 控件的代码中创建的所有视图:

在此处输入图像描述

This is how it looks like on the device:这是它在设备上的样子:

在此处输入图像描述

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:还有一件事,正如您在第一个屏幕截图中看到的那样,我的高度限制 >= 60,但如果我删除此限制,我的自定义 slider 将根本不可见:

在此处输入图像描述

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:这是我的自定义 slider 代码,但我想我搞砸了更新父视图(UITableViewCell)的约束或计算设置父视图的(UITableViewCell)内容高度:

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()
}

As per OP...根据 OP...

You're missing constraints that are needed to auto-size the height of the cell.您缺少自动调整单元格高度所需的约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM