1

I'm doing a storyboardUI app. One part of the UI design is kind of like this:

enter image description here

I want the label position follows the slider position all the time, like below:

enter image description here

How can I do it?

1 Answers1

5

Assign this class to UISlider.

In this class created one label and change the position according to the slider thumb.

class ThumbTextSlider: UISlider {
    private var thumbTextLabel: UILabel = UILabel()
    
    private var thumbFrame: CGRect {
        return thumbRect(forBounds: bounds, trackRect: trackRect(forBounds: bounds), value: value)
    }
    
    private lazy var thumbView: UIView = {
        let thumb = UIView()
        return thumb
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        thumbTextLabel.frame = CGRect(x: thumbFrame.origin.x, y: thumbFrame.maxY - 5, width: thumbFrame.size.width, height: 30)
        self.setValue()
    }
    
    private func setValue() {
        thumbTextLabel.text = String(format: "%0.2f", self.value)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        addSubview(thumbTextLabel)
        thumbTextLabel.textAlignment = .center
        thumbTextLabel.textColor = .blue
        thumbTextLabel.layer.zPosition = layer.zPosition + 1
        thumbTextLabel.adjustsFontSizeToFitWidth = true
    }
}

enter image description here

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • Thanks for the answer. This looks promising. However when I paste the code into the project the the label still hasn't been shown. – Richard Whatever Apr 26 '21 at 06:39
  • 1
    Have you assigned this class to the UISlider controller? If yes then have you added any height constraint to the slider? – Raja Kishan Apr 26 '21 at 06:44
  • terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key UISliderController.' terminating with uncaught exception of type NSException CoreSimulator 732.18.6 - Device: iPhone SE (2nd generation) (EBD31BDC-336E-4C13-A4B5-31EF698284DD) - Runtime: iOS 14.4 (18D46) - DeviceType: iPhone SE (2nd generation) (lldb) – Richard Whatever Apr 26 '21 at 07:14
  • after a couple of try the app just wouldn't let me to go anymore – Richard Whatever Apr 26 '21 at 07:14
  • Can you create one demo project and take a one slider and assign this class and check. might be a problem with your code. – Raja Kishan Apr 26 '21 at 07:18
  • I got it work now. Just one more question about how do I set the initial value for the UI slider? – Richard Whatever Apr 26 '21 at 08:00
  • 1
    If you feel this ans is useful then you can accept this and for your second question please check this: https://stackoverflow.com/a/65274464/14733292 – Raja Kishan Apr 26 '21 at 08:04
  • 1
    thanks so much for the help Raja. Hope you have a nice day. – Richard Whatever Apr 26 '21 at 08:23