2011-11-04 131 views

回答

2

下次使用方法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]; 

,並創建子這樣

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

這不起作用 – user997128

+0

奇怪,也許我不明白你想要什麼? – SAKrisT

+0

再次,在這裏不起作用。我猜只是提供的代碼是iOS5? (我使用的是iOS4,並且只有iOS5的UISlider) – Adam

23

您需要繼承滑塊和覆蓋trackRectForBounds:方法,像這樣:

- (CGRect)trackRectForBounds:(CGRect)bounds 
{ 
    return bounds; 
} 
1
//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]; 
2

對於那些希望看到一些工作的代碼改變軌道大小。

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

唯一剩下的就是改變故事板內的類:

storyboardstuff

您可以繼續使用搜索條的行動和出口對象類型UISlider,除非你想添加一些更多的自定義東西到你的滑塊。

5

爲雨燕最簡單的辦法:

class TBSlider: UISlider { 

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