UISlider track height
Asked Answered
C

2

5

I have implemented UISlider and I need to adjust track height. I found this but its not working.

self.slider.trackRect(forBounds: CGRect(x: 0, y: 0, width: 100, height: 100))

enter image description here

Chapell answered 31/1, 2017 at 13:45 Comment(0)
D
0

found this but its not working

Because you're using it wrong. You don't call it. You subclass, and override it.

However, the right approach is to supply new images for the track, with setMinimumTrackImage and setMaximumTrackImage.

Disbud answered 31/1, 2017 at 13:56 Comment(3)
Example: github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…Disbud
Did you try your "solution" it causes a problem when thumb image doesn't overlap the min and max slider imageSweltering
Well it tells us all what's wrong, but doesn't really help with getting things to work.Landseer
C
15

Ok...I used matt's answer + provided example and I customized the code for my needs.

let coinEnd = UIImage(/*HERE_LEFT_BLANK_IMG*/).resizableImage(withCapInsets:
        UIEdgeInsetsMake(0,7,0,7), resizingMode: .stretch)
.
.
.
.

override func trackRect(forBounds bounds: CGRect) -> CGRect {
    var result = super.trackRect(forBounds: bounds)
    result.origin.x = 0
    result.size.width = bounds.size.width
    result.size.height = 10 //added height for desired effect
    return result
}

override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
    return super.thumbRect(forBounds:
        bounds, trackRect: rect, value: value)
        .offsetBy(dx: 0/*Set_0_value_to_center_thumb*/, dy: 0)
}

then inherid this to my Slider and woala.

enter image description here

This is my desired result.

enter image description here

All hail matt...thank you.

Chapell answered 31/1, 2017 at 14:44 Comment(0)
D
0

found this but its not working

Because you're using it wrong. You don't call it. You subclass, and override it.

However, the right approach is to supply new images for the track, with setMinimumTrackImage and setMaximumTrackImage.

Disbud answered 31/1, 2017 at 13:56 Comment(3)
Example: github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…Disbud
Did you try your "solution" it causes a problem when thumb image doesn't overlap the min and max slider imageSweltering
Well it tells us all what's wrong, but doesn't really help with getting things to work.Landseer

© 2022 - 2024 — McMap. All rights reserved.