How to customise UISlider height

后端 未结 9 1645
Happy的楠姐
Happy的楠姐 2020-12-06 05:42

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

相关标签:
9条回答
  • 2020-12-06 06:07

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

    - (CGRect)trackRectForBounds:(CGRect)bounds
    

    Just return the new CGRect here.

    0 讨论(0)
  • 2020-12-06 06:10

    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
           }
    }
    
    0 讨论(0)
  • 2020-12-06 06:10

    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
    }
    
    0 讨论(0)
提交回复
热议问题