How to customise UISlider height

后端 未结 9 1644
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 05:49

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

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

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

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

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

    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.

    0 讨论(0)
提交回复
热议问题