UISlider没有占据全宽

时间:2016-02-10 03:19:47

标签: ios swift uislider

我已经制作了一个完整的宽度(两边都限制为边缘0),但是有几个像素未使用,请参见下图:

enter image description here

我听说这是因为留下拇指滑块的空间?

有没有办法填补这个小洞?

我考虑过放置一个小视图来填充它,但这看起来非常粗糙和粗糙:(

谢谢!

滑块设置:

class TrackProgressSlider : BiggerThumbTouchUISlider {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.minimumValue = 0
        self.maximumValue = 100
        self.value = 0
        self.backgroundColor = UIColor.clearColor()
        self.transform = CGAffineTransformMakeScale(1.0, 2.0)
        self.setThumbImage(UIImage(named: "progress-marker-half")!, forState: UIControlState.Normal)
        self.minimumTrackTintColor = UIColor(hexString: "#f2f2f2")
        self.maximumTrackTintColor = UIColor.clearColor()

    }
}

1 个答案:

答案 0 :(得分:2)

利用trackRectForBounds(_ :)

class CustomSlider: UISlider {
  override func trackRectForBounds(bounds: CGRect) -> CGRect {
    var trackRect = super.trackRectForBounds(bounds)
    trackRect.size.width = bounds.width
    trackRect.origin.x = 0
    return trackRect
  } 
}