I'm working on an app and I have a custom UISlider
.
However, I'm having some issues on how to make the default thumb to appear smaller like the ones in the iOS control center.
Note that I want the same iOS thumb, not a custom thumb image. So far, I've tried thumbRect(forBounds...)
but no luck.
Any suggestions?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
Download an image similar to the one you have in the iOS control centre.
Scale it to the size you want (20x20 or even smaller) and save it in your assets.
Paste the following code in viewDidLoad:
You can't change the size of the default thumb image, but
UISlider
has a methodsetThumbImage(_:for:)
that will allow you to pass a similar, smaller image.In your view controller
viewDidLoad
:See Customizing the Slider’s Appearance of the API Reference.
On iOS10, the default thumb image appear to be no more than a bordered white circle with a thin shadow dropped under (if you don't set the
thumbTintColor
).I use this snippet to generate a similar image that can be scaled down ;)
Here's an option for you, use a CGAffineTransform.
Swift 4
This will change the width and height of the slider, so you may need to realign it with your interface.
If you want to change Thumb Image as well as Tint Color then both can't be possible; There is one workaround of this issue. Create circular image programmatically and change the color of the image and assign that image to slider thumb; In this way, you can also resize the image as well as color.
Here is the fully working sample code:
You can call this function like this:
This method
setSliderThumbTintColor
will be called onvalueChanged
event of UISlider.I created a
UISlider
subclass that allows to change the thumb size as well as track size, all without using images.Result: