ios - UISlider:更改了高度,但最大值闪烁

标签 ios swift uiview uislider

我自定义了一个 UISlider 并且一切正常,除非我将 slider 拖动到 100%。然后圆形的帽子被方形取代。

以下是我如何自定义 slider :

@IBInspectable var trackHeight: CGFloat = 14

override func trackRect(forBounds bounds: CGRect) -> CGRect {
    return CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: trackHeight))
}

98% 图片:

enter image description here

100% 图片:

enter image description here

最佳答案

您可以通过将minimumTrackTintColormaximumTrackTintColor设置到UIColor.clear中来移除轨道背景,然后自己绘制轨道背景。

例如:

class CustomSlider: UISlider {

    override func trackRect(forBounds bounds: CGRect) -> CGRect {
        return bounds
    }

    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }

        context.setFillColor(UIColor.green.cgColor)
        let path = UIBezierPath(roundedRect: rect, cornerRadius: rect.size.height / 2).cgPath
        context.addPath(path)
        context.fillPath()
    }

}

关于ios - UISlider:更改了高度,但最大值闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53262990/

相关文章:

ios - 在iphone或ipad的显示屏中央显示一个subview

ios - 如何从 Objective C 中的另一个值替换 NSDictionary 值?

ios - 带有分钟和秒的日期选择器

ios - 如何快速检查当前时间是否大于 jwt exp(expiration time)?

ios - 在另一个 UIViewController 之上呈现一个模糊的 UIViewController

ios - 你如何在 swift 中从 JSON 中排序 NSMutableArray?

iphone - UIDatePicker 不显示适当的时间

swift - 是否可以在使用 SpriteKit 的应用程序上使用 Xcode UI 测试?

ios - 从 self.view 中删除所有子类 UIView

ios - 我在 IB 中添加的方形 uiview 在设备旋转时不会跳转 - 如何在代码中重复相同的操作?