I am trying to make a custom SwiftUI Slider with ticks and a custom thumb image. It works well except for that the thumb image is not aligned with the tick marks. How can I make them aligned?
init() {
let ticker: UIImage = UIImage(named: "ticker")?.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 90)) ?? UIImage()
UISlider.appearance().setThumbImage(ticker, for: .normal)
}
Slider:
ZStack {
HStack {
ForEach(0...10, id: \.self) { _ in
Text("I")
.frame(maxWidth: .infinity)
.foregroundColor(ColorStyle(colorAsset: .lightGrey).color)
}
Slider(value: $viewModel.sliderValue, in: minSlider...maxSlider, step: sliderStep)
.tint(ColorStyle(colorAsset: .lightGrey).color)
}
working on a generic slider builder to reduce repeated coder, optimised for iPad (iOS). The trick is to allow for the way the track length is reduced by the thumb width, so full range is a little less than the frame width. The attached code is incomplete - need to set up @environment for passed in variables. The pink borders are just to highlight the frames for you.