UIslider thumb image doesn't start from the beginning
What you're seeing is normal. The slider leaves some extra space at both ends, so that the thumb is at minimum or maximum value when the edge of the thumb is at the end of the slider frame.
Look at these sliders. They have the same horizontal positions and width:
The first slider's thumb is as far to the left as it will go. It doesn't go further left, outside the frame of the track; it stops when its edge hits the the frame of the track. That is zero.
If you don't like where the thumb image is being drawn in relation to the overall track, you'll need to subclass UISlider and implement thumbRectForBounds:trackRect:value:
.
How to make a custom UISlider iOS?
You'll have to do a bit of work, but you shouldn't need to build a complete solution from scratch.
Adding your labels will be trivial - pretty basic usage of UISlider
"Stretching" a track image (or tint color) left and right of center as your animation shows will be a little tricky, as the UISlider
control has only a track / background image and a "current" image that stretches on the left-hand side.
One approach would be to set the UISlider
track images to transparent or hidden and then "overlay" the slider on top of your "full line" and your "blue line". When the slider value changes (as it is dragged), change the frame of the blue line.
Using auto-layout and constraints would make that pretty straight-forward.
Edit: A really quick example. The "thumb" is purposefully moved below the "custom slider bar" to show the images...
And, this is the code to get that:
import UIKit
class SliderViewController: UIViewController {
// @IBOutlets needed, but I'm just showing the "code" here
// ...
@IBAction func sliderValueChanged(_ sender: Any) {
if let s = sender as? UISlider {
updateSlider(pct: CGFloat(s.value))
}
}
func updateSlider(pct: CGFloat) -> Void {
let p = (pct - 0.5) * 2.0
theValueLabel.text = String(format: "%0.1f", p * 10)
leftSideValueWidth.constant = p >= 0.0 ? 0.0 : (leftSideHolderView.frame.size.width * (-p))
rightSideValueWidth.constant = p >= 0.0 ? (rightSideHolderView.frame.size.width * p) : 0.0
}
override func viewDidLoad() {
super.viewDidLoad()
// init slider to center
updateSlider(pct: 0.5)
// set default left- and right-side "track" images to empty images
theSlider.setMinimumTrackImage(UIImage(), for: .normal)
theSlider.setMaximumTrackImage(UIImage(), for: .normal)
}
}
Black UIView
for the frame... Gray UIView
for the "slider bar"... two transparent UIView
for the left- / right- side framing... two blue UIView
- one for each side. And a standard UISlider
control. That's about it.
You could also set a custom Thumb image, if the default one is too big for your needs.
Related Topics
Value' Is Inaccessible Due to 'Internal' Protection Level
Prepare for Segue with Array - Xcode 8.0 Swift 3.0
How to Notify a Queue in Swift (Gcd)
Swift Uikit Dynamics Add Collision Boundary. After Rotation Does Not Work Correctly
Need Clarification for Swift Type Properties
How to Move a Model and Generate Its Collision Shape at the Same Time
How to Comprehend the "First-Class Function" in Swift
Uibutton Causing Unrecognized Selector Sent to Instance
Generate Labels and Align in Middle
Swift - Creating Shadow with 2 Different Colours for Imageview
Show Datepicker Hourandminute on Print Statement Swiftui
Swift Optional Property Using Kvc Causes Crash
How to Use the Optional Variable in a Ternary Conditional Operator
How to Add Nil Value to Swift Dictionary
Alamofire 4 Error Request 'Extra Argument in Call'
How to Resume a Published Timer in Swiftui After Navigating to a Different Page