ios 自定义 UISlider(跟踪图像高度)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8016496/
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-30 15:14:34  来源:igfitidea点击:

Customize UISlider (track image height)

iphoneioscocoa-touchuislider

提问by user997128

I'm customizing a UISlider. I could set a custom thumb image that is higher than the usual thumb however I could not make the track higher when setting a higher minimum track image but the track height remained the same. It should be possible as in the iPod/Music App on the iPad the volume slider is also higher as the usual slider as you can see here:

我正在定制一个UISlider. 我可以设置一个比通常拇指更高的自定义拇指图像,但是当设置更高的最小轨道图像时我无法使轨道更高,但轨道高度保持不变。应该可以,因为在 iPad 上的 iPod/音乐应用程序中,音量滑块也比通常的滑块高,如下所示:


(source: cocoia.com)


(来源:cocoia.com

回答by antalkerekes

You need to subclass the slider and override the trackRectForBounds:method, like this:

您需要子类化滑块并覆盖该trackRectForBounds:方法,如下所示:

- (CGRect)trackRectForBounds:(CGRect)bounds
{
    return bounds;
}

回答by Bart?omiej Semańczyk

The simplest solution for Swift:

Swift 最简单的解决方案:

class TBSlider: UISlider {

    override func trackRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectMake(0, 0, bounds.size.width, 4)
    }
}

回答by CularBytes

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

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

class CustomUISlider : UISlider
{        
    override func trackRectForBounds(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.trackRectForBounds(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"), forState: .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 SAKrisT

use next methods setThumbImage, setMinimumTrackImage, setMaximumTrackImage

使用下一个方法setThumbImagesetMinimumTrackImagesetMaximumTrackImage

[self setThumbImage:[UIImage imageNamed:@"switchThumb.png"] forState:UIControlStateNormal];
    [self setMinimumTrackImage:[UIImage imageNamed:@"switchBlueBg.png"] forState:UIControlStateNormal];
    [self setMaximumTrackImage:[UIImage imageNamed:@"switchOffPlain.png"] forState:UIControlStateNormal];

and create subclass like this

并像这样创建子类

- (id) initWithFrame: (CGRect)rect{
    if ((self=[super initWithFrame:CGRectMake(rect.origin.x,rect.origin.y,90,27)])){
        [self awakeFromNib];
    }
    return self;
}

回答by NANNAV

//use this code

 UIImage *volumeLeftTrackImage = [[UIImage imageNamed: @"video_payer_scroll_selection.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
        UIImage *volumeRightTrackImage= [[UIImage imageNamed: @"video_bar_bg.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
        [volumeslider setMinimumTrackImage: volumeLeftTrackImage  forState: UIControlStateNormal];
        [volumeslider setMaximumTrackImage: volumeRightTrackImage  forState: UIControlStateNormal];
        [volumeslider setThumbImage:[UIImage imageNamed:@"sound_bar_btn.png"] forState:UIControlStateNormal];
        [tempview addSubview:volumeslider];