17

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 bar line.

I tried something like this but don't work:

let customBounds = CGRect(origin: bounds.origin, 
                          size: CGSize(width: bounds.size.width, height: 15.0))
feedbackSlider.trackRectForBounds(customBounds)

Thanks

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Nik
  • 1,517
  • 5
  • 14
  • 19

9 Answers9

24

i hope that you want edit it in storyboard, and only the line size, use it in your custom UISlider

class CustomSlide: UISlider {

     @IBInspectable var trackHeight: CGFloat = 2

     override func trackRectForBounds(bounds: CGRect) -> CGRect {
         //set your bounds here
         return CGRect(origin: bounds.origin, size: CGSizeMake(bounds.width, trackHeight))



       }
}
Eduardo Oliveros
  • 863
  • 9
  • 20
Learn Swift
  • 570
  • 5
  • 15
14

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
       }
}
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
nCod3d
  • 667
  • 1
  • 10
  • 12
9

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
}
ronan
  • 1,611
  • 13
  • 20
  • 13
    when i use this code, the height of the slider successfully changes. but when the slider gets to the end, the rounded corner 'flashes' square. it seems this code is not accounting for the rounded corner at the end of a slider. any idea how to do that? – haplo1384 Feb 03 '16 at 15:53
  • same here. Any fix for that? – netshark1000 Nov 12 '18 at 13:10
6

Swift 3

class MySlide: UISlider {

    @IBInspectable var height: CGFloat = 2        
    override func trackRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: height))
    }
}
David Seek
  • 16,783
  • 19
  • 105
  • 136
6

Swift 4 version without thumbImage.

class CustomSlider: UISlider {
  @IBInspectable var trackHeight: CGFloat = 2

  override func trackRect(forBounds bounds: CGRect) -> CGRect {
    return CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: trackHeight))
  }
}
Coupz
  • 531
  • 7
  • 11
2

I think the answers above have a flaw: the origin of the track rect must be offset to account for the change in height of the track, otherwise it will show up off-center. Here is the way I did it:

class CustomSlider: UISlider {

@IBInspectable var sliderTrackHeight : CGFloat = 2

    override func trackRect(forBounds bounds: CGRect) -> CGRect {
        let originalRect = super.trackRect(forBounds: bounds)
        return CGRect(origin: CGPoint(x: originalRect.origin.x, y: originalRect.origin.y + (sliderTrackHeight / 2)), size: CGSize(width: bounds.width, height: sliderTrackHeight))
    } 
}
Yariv Adam
  • 844
  • 9
  • 24
1

U can easily get it done by subclassing UISlider. see following code

class CustomUISlider : UISlider
{        
    override func trackRectForBounds(bounds: CGRect) -> CGRect {
    //set your bounds here
    return bounds

   }
}
iAnurag
  • 9,286
  • 3
  • 31
  • 48
0

You should be able to subclass the UISlider and then implement:

- (CGRect)trackRectForBounds:(CGRect)bounds

Just return the new CGRect here.

pnavk
  • 4,552
  • 2
  • 19
  • 43
-1

If you're using autolayout you can set a height constraint on the UISlider in the storyboard. if you need to change it at runtime - create an IBOutlet for the constraint and modify its .constant value.

Robert J. Clegg
  • 7,231
  • 9
  • 47
  • 99