ios 使 UISlider 高度更大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23320179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:01:30  来源:igfitidea点击:

Make UISlider height larger?

iosobjective-ccocoa-touchuislider

提问by Josue Espinosa

I've been searching for a way to make the UISliderprogress bar taller, that is, increase the height. However, I haven't had any success. I don't want to use a custom image or anything, just make it taller, so the UISliderdoesn't look so thin. Am I missing something super obvious?

一直在寻找让UISlider进度条变高的方法,也就是增加高度。但是,我没有取得任何成功。我不想使用自定义图像或任何东西,只是让它更高,所以UISlider看起来不那么薄。我错过了一些非常明显的东西吗?

采纳答案by Josue Espinosa

I found what I was looking for. The following method just needs to be edited in a subclass.

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

- (CGRect)trackRectForBounds:(CGRect)bounds{
    CGRect customBounds = ...
    return customBounds;
}

回答by kwahn

The accepted answer will undesirably change the slider's width in some cases, like if you're using a minimumValueImageand maximumValueImage. If you only want to change the height and leave everything else alone, then use this code:

在某些情况下,接受的答案会不希望地改变滑块的宽度,例如如果您使用 aminimumValueImagemaximumValueImage。如果您只想更改高度并保留其他所有内容,请使用以下代码:

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

回答by wils

Here's my recent swifty implementation, building on CularBytes's ...

这是我最近的 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
        )
    }
}

Use this on a UISlider in a storyboard by setting its custom class Custom class setting

通过设置其自定义类在故事板中的 UISlider 上使用它 自定义类设置

The IBInspectable allows you to set the height from the storyboard Height from storyboard

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

回答by CularBytes

For those that would like to see some working code for changing the track size.

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

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()
    }
}

Only thing left is changing the class inside the storyboard:

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

storyboardstuff

故事板的东西

You can keep using your seekbar action and outlet to the object type UISlider, unless you want to add some more custom stuff to your slider.

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

回答by Jelle

You could play with this, see what happens:

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

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