1

I need to develop custom UISlider using swift. The problem is I don't know how to make sure label following slider's thumbnail like following below:

enter image description here

My code so far

import Foundation
import UIKit

class Slider: UISlider {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.borderColor = UIColor.redColor().CGColor
        self.layer.borderWidth = 0.2
        self.tintColor = UIColor.redColor()
    }
}
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • possible duplicate of [UILabel Over UISlider Thumb](http://stackoverflow.com/questions/3510961/uilabel-over-uislider-thumb) – Nikita Leonov Jul 01 '15 at 07:27
  • You can't use that method if you're using Auto Layout, so that answer doesn't apply to all cases. – Cihan Tek Jul 01 '15 at 07:29
  • @CihanTek you can not use Auto Layout with UISlider so it apply to all cases. – Nikita Leonov Jul 01 '15 at 07:39
  • Ah, sorry you're right. I've thought of it as a fully custom-made slider that doesn't inherit from UISlider. But you still can't use that method because you can't modify the frames directly while using auto layout. – Cihan Tek Jul 01 '15 at 07:41
  • I'd like to make clear that if you are using auto layout somewhere you are using it everywhere even if you do not add specific constraints. In this case autoresizing mask will be converted into constraint. – Andrea Jul 01 '15 at 07:42
  • i'm using autolayout sir. – Nurdin Jul 01 '15 at 07:43
  • 1
    @Andrea you are right, but the issue here that you can not connect your constraints to internals of UISlider, only to top view. So all constraints should be based on a rect data provided, but not on internal layout. – Nikita Leonov Jul 01 '15 at 07:46
  • @NikitaLeonov correct! – Andrea Jul 01 '15 at 07:47

2 Answers2

0

Probably is worth to take a look at this method, this can't be called directly but you can use it while subclassing

func thumbRectForBounds(_ bounds: CGRect,
              trackRect rect: CGRect,
                  value value: Float) -> CGRect

More info also on this question and here

Community
  • 1
  • 1
Andrea
  • 26,120
  • 10
  • 85
  • 131
0

I had the exact thing for audio files. The protocol is used to inform the player when user changes the value of the slider. func updateSliderPosition(position: Float) is called every half second by the player to change position of the slider current value.

import UIKit

protocol AudioSliderControlDelegate: class
{
    func audioSliderControlDdiChangeValue(sender: AudioSliderControl, value: Float)
}
class AudioSliderControl: UIControl
{
    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var currentPositionLabel: UILabel!
    @IBOutlet weak var totalDurationLabel: UILabel!

    weak var delegate: AudioSliderControlDelegate?

    private var totalLenght: Double?

    override func awakeFromNib()
    {
        super.awakeFromNib()
        backgroundColor = UIColor.clevooOrange()
        slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: .ValueChanged)
        self.addSeperatorToTop()
    }

    //MARK:
    //MARK: - Setup
    func setupForDuration(duration: Double)
    {
        totalLenght = duration
        currentPositionLabel.text = Double(0).formattedDuration()
        totalDurationLabel.text = (duration + 0.5).formattedDuration()
        slider.setValue(0, animated: false)
    }

    func updateSliderPosition(position: Float)
    {
        self.currentPositionLabel.text = (totalLenght! * Double(position)).formattedDuration()
        self.slider.value = position
    }

    //MARK:
    //MARK: - Notification

    func sliderValueChanged(slider: UISlider)
    {
        self.currentPositionLabel.text = (totalLenght! * Double(slider.value)).formattedDuration()
        delegate?.audioSliderControlDdiChangeValue(self, value: slider.value)
    }
}
Nurdin
  • 23,382
  • 43
  • 130
  • 308
Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79