Can no longer get subviews from UISlider. iOS 14

I am trying a add a subview to the UISlider Thumb. This worked until iOS 14

Code Block  
        if let redView = redO.subviews.last as? UIImageView  {
            let label = UILabel(frame: redView.bounds)
            label.backgroundColor = .clear
            label.textAlignment = .center
            redView.addSubview(label)
 }
 

This used to work. Does any one know how to get the Thumb subview in iOS 14?
Answered by Claude31 in 652074022
Have you checked that
Code Block
if let handleView = foregroundSlider.subviews.last as? UIImageView {

does not return nil ?
I am also really surprised it worked in iOS 12…

how to add label to slider thumbs in iOS 14?

My understanding is that you should create an image from the label
https://stackoverflow.com/questions/11867152/how-to-create-an-image-from-uilabel
and then call
Code Block
setThumbImage(image, for: .normal)

This used to work. Does any one know how to get the Thumb subview in iOS 14?

I don't see it working in iOS 12.4.
What do you expect to see ?
Your label is empty. So even if it worked, you should see nothing.
Is it a code of yours or a code you copied somewhere ?

Effectively, UISlider seems to have changed between iOS 12 end 14.
I added a print(mySlider.subviews.count) to the code.
  • in iOS 12.4, I get 0 : no subview ; so last should return nil: code will not go through this if let redView at line 1.

  • In iOS 14.2, I get 1, UISlider has just one subview (the whole frame)

I also printed all the subviews.
Code Block
print("Count", mySlider.subviews.count)
for v in mySlider.subviews {
print("subview", v)
}
  • iOS 12.4: of course get nothing

  • iOS 14.2, I get the following:

subview <_UISlideriOSVisualElement: 0x7f906fe0f240; frame = (0 0; 118 30); opaque = NO; autoresize = W+H; layer = <CALayer: 0x600003666880>>
It is the whole UISlider.

To have a special thumb, you can do the following
Code Block
let image : UIImage = UIImage(systemName: "pencil.circle.fill")!
mySlider.setThumbImage(image, for: .normal)

Take care: cannot change color and image.


From UISlider doc:

Use either a custom tint color or a custom image, but not both. When customizing slider appearance with images or tint, use one option or the other, but not both. Conflicting settings for track and thumb appearance are resolved in favor of the most recently set value. For example, setting a new minimum track image for any state clears any custom tint color you may have provided for minimum track images. Similarly, setting the thumb tint color removes any custom thumb images associated with the slider.


For more details:
https://stackoverflow.com/questions/6047565/is-that-possible-to-add-a-label-to-the-uisliders-thumb-image
Trying to add label to UISlider thumb. I use this label to display slider value. The following is more complete code.
The line slider.subviews.last as? UIImageView used to return the thumb view. This worked until iOS 14 Does anyone know how to add label to slider thumbs in iOS 14?
Code Block
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
sliderLabel?.text = ""
if let handleView = foregroundSlider.subviews.last as? UIImageView {
let label = UILabel(frame: handleView.bounds)
label.backgroundColor = .clear
label.textAlignment = .center
label.font = label.font.withSize(12)
label.text = String(Int(foregroundSlider.value * 100))
label.textColor = .white
handleView.addSubview(label)
self.sliderLabel = label
}
}


Accepted Answer
Have you checked that
Code Block
if let handleView = foregroundSlider.subviews.last as? UIImageView {

does not return nil ?
I am also really surprised it worked in iOS 12…

how to add label to slider thumbs in iOS 14?

My understanding is that you should create an image from the label
https://stackoverflow.com/questions/11867152/how-to-create-an-image-from-uilabel
and then call
Code Block
setThumbImage(image, for: .normal)

Claude31 Thank you. Turning label into image worked.
Thanks for feedback.

Don't forget to close the thread on the correct answer.
Can no longer get subviews from UISlider. iOS 14
 
 
Q