I have project in which have to customise UISlider
element.
I wondering if someone knows how to change, or is it possible to change height of UISlide
You should be able to subclass the UISlider and then implement:
- (CGRect)trackRectForBounds:(CGRect)bounds
Just return the new CGRect here.
Overriding trackRect
is a way to go, however if you're using additional UISlider
's views like minimumValueImage, maximumValueImage
you would also need to take their bound into account, otherwise they will overlap with slider’s track. As a shortcut you can simply use super
's func:
Fixed version.
Swift 3+
@IBDesignable
class CustomSlider: UISlider {
/// custom slider track height
@IBInspectable var trackHeight: CGFloat = 3
override func trackRect(forBounds bounds: CGRect) -> CGRect {
// Use properly calculated rect
var newRect = super.trackRect(forBounds: bounds)
newRect.size.height = trackHeight
return newRect
}
}
You can override a method in you custom slider
For Objective-C
- (CGRect)trackRectForBounds:(CGRect)bounds {
CGRect rect = CGRectMake(0, 0, 100, 30);//change it to any size you want
return rect;
}
For Swift
override func trackRectForBounds(bounds: CGRect) -> CGRect {
var rect:CGRect = CGRectMake(0, 0, 100, 30)
return rect
}