Susanna
Susanna

Reputation: 771

Set the thumbImage and watch the slider-bar disappear?

[sld1 setThumbImage:[UIImage imageNamed:@"Blue.png"] forState:UIControlStateHighlighted];
[sld1 setThumbImage:[UIImage imageNamed:@"Blue.png"] forState:UIControlStateNormal     ];

Would that cause the slider-bar image to disappear? Mine are all gone. (The thumb-image displays just fine.)

Apple's docs make it sounds like I can use any ONE of above 2 lines of code. (But I guess I really need both.)

And I can't find anything about "you must always do all 4":

Set the normal-state image.
Set the highlighted-state image.
Set the setMinimumTrackImage.
Set the setMaximumTrackImage.

Upvotes: 0

Views: 988

Answers (1)

RickiG
RickiG

Reputation: 11390

If you start out customizing the UISlider with graphics, you must set at least 3 pieces of graphics. Right and left side of the trackbar and the thumb image:

Here is how I did it the last time I used the UISlider:

    CGRect frame = CGRectMake(20.0f, 6.0, 280.0f, 20.0f);
    UISlider *customSlider = [[UISlider alloc] initWithFrame:frame];
    [customSlider addTarget:self action:@selector(sliderMove:) forControlEvents:UIControlEventValueChanged];
    [customSlider addTarget:self action:@selector(sliderStart:) forControlEvents:UIControlEventTouchDown];
    [customSlider addTarget:self action:@selector(sliderEnd:) forControlEvents:UIControlEventTouchUpInside];        
    [customSlider addTarget:self action:@selector(sliderEnd:) forControlEvents:UIControlEventTouchUpOutside];   

    // in case the parent view draws with a custom color or gradient, use a transparent color
    customSlider.backgroundColor = [UIColor clearColor];    
    UIImage *stetchLeftTrack = [[UIImage imageNamed:@"slider.png"]
                                stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];

     UIImage *stetchRightTrack = [[UIImage imageNamed:@"slider_right.png"]
                                stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];

    [customSlider setThumbImage: [UIImage imageNamed:@"slider-knop.png"] forState:UIControlStateNormal];
    [customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
    [customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];

I my optics it does not make sense to have a highlighted state for the UISlider, or any other state, your finger will likely cover the thumb image, so the user will never see it. I am not sure it just extends from UIButton and maybe it can't handle it?

Upvotes: 1

Related Questions