OGeek|极客世界-中国程序员成长平台

标题: ios - UISlider 缩略图位置偏移 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:19
标题: ios - UISlider 缩略图位置偏移

我正在尝试使用自定义图像实现标准 UISlider。

目前看起来是这样

enter image description here

但我需要它看起来像这样

enter image description here

如何将缩略图向上偏移,以便只有尖端几乎不接触线?



Best Answer-推荐答案


如果您想更改拇指图像的偏移量,您必须在自定义 UISlider 子类中覆盖 thumbRectForBounds:trackRect:value:

class CustomSlider: UISlider {
    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
        let originalRect = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
        let yOffset: CGFloat = 16 // any value you want
        let newY = originalRect.origin.y - yOffset

        return CGRect(x: originalRect.origin.x, y: newY, width: originalRect.width, height: originalRect.height)
    }
}

关于ios - UISlider 缩略图位置偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221860/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4