【问题标题】:Make UISlider height larger?让 UISlider 高度变大?
【发布时间】:2014-06-12 18:22:01
【问题描述】:

我一直在寻找使UISlider 进度条更高的方法,例如增加滑块的高度,但找不到任何东西。我不想使用自定义图像或任何东西,只是让它更高,所以UISlider 看起来不那么薄。有没有一种我想念的简单方法来做到这一点?

【问题讨论】:

标签: ios objective-c swift cocoa-touch uislider


【解决方案1】:

在某些情况下,接受的答案会改变滑块的宽度,例如您使用minimumValueImagemaximumValueImage。如果您只想更改高度而忽略其他所有内容,请使用以下代码:

override func trackRect(forBounds bounds: CGRect) -> CGRect {
   var newBounds = super.trackRect(forBounds: bounds)
   newBounds.size.height = 12
   return newBounds
}

【讨论】:

  • 作为一个额外步骤,您可以添加newBounds.origin.y -= 6(或您的新值除以 2),以确保它以新的厚度值在 Y 中居中。
  • @FernandoMata 不完全是 6... 需要从 y 中减去 (newHeight-originalHeight)/2
【解决方案2】:

这是我最近的 swifty 实现,基于 CularBytes 的...

open class CustomSlider : UISlider {
    @IBInspectable open var trackWidth:CGFloat = 2 {
        didSet {setNeedsDisplay()}
    }

    override open func trackRect(forBounds bounds: CGRect) -> CGRect {
        let defaultBounds = super.trackRect(forBounds: bounds)
        return CGRect(
            x: defaultBounds.origin.x,
            y: defaultBounds.origin.y + defaultBounds.size.height/2 - trackWidth/2,
            width: defaultBounds.size.width,
            height: trackWidth
        )
    }
}

通过设置自定义类在情节提要中的 UISlider 上使用它

IBInspectable 允许您设置故事板的高度

【讨论】:

  • @IBInspectable 是一个惊人的添加 ?
  • 感谢兄弟。
【解决方案3】:

对于那些希望看到一些更改轨道大小的工作代码的人。

class CustomUISlider : UISlider {

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

        //keeps original origin and width, changes height, you get the idea
        let customBounds = CGRect(origin: bounds.origin, size: CGSize(width: bounds.size.width, height: 5.0))
        super.trackRect(forBounds: customBounds)
        return customBounds
    }

    //while we are here, why not change the image here as well? (bonus material)
    override func awakeFromNib() {
        self.setThumbImage(UIImage(named: "customThumb"), for: .normal)
        super.awakeFromNib()
    }
}

唯一剩下的就是改变故事板中的类:

您可以继续使用您的搜索栏操作和出口到对象类型 UISlider,除非您想向滑块添加更多自定义内容。

【讨论】:

  • 嗯。这行得通,但它使UISlider 的对齐变得相当混乱,setThumbImage 函数不起作用。
  • 请详细说明为什么它会弄乱对齐?你确定 setThumbImage 不起作用吗?我刚刚签入了 Xcode 7.0 beta、Swift 2.0
  • 我认为原点没有垂直居中(如默认的 UISlider)。我只是添加了一个顶级约束来应对它。
  • @BrightFuture 你确定forBounds bounds 是swift 3 吗?完整的代码是否适用于 swift 3?我目前没有进入 swift 3 开发,但只是想验证一下,看起来很奇怪。
  • @CularBytes 是的,代码是由 Xcode 转换的
【解决方案4】:

我找到了我要找的东西。下面的方法只需要在子类中编辑即可。

override func trackRect(forBounds bounds: CGRect) -> CGRect {
   var customBounds = super.trackRect(forBounds: bounds)
   customBounds.size.height = ...
   return customBounds
}

【讨论】:

  • 这很好(并且有效),但不要忘记调用 bounds = [super trackRectForBounds:bounds];否则它会改变滑块框架..
  • @Cutetare,如果你想通过自己的界限,为什么需要调用 super trackRectForBounds: 方法?
【解决方案5】:

你可以玩这个,看看会发生什么:

slider.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 2.0);

【讨论】:

  • 因为这会改变整个框架,而不仅仅是问题所述的条形,并且不会重新调整角半径或拇指滑块框架,所以一切看起来都不成比例。
猜你喜欢
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
相关资源
最近更新 更多