Custom UISlider

2020-05-27 11:24发布

问题:

How to customize UISlider?(change style,background,...)

回答1:

You can go through this tutorial for customizing the controls.

For customizing UISlider,use this piece of code from this tutorial.

UIImage *minImage = [[UIImage imageNamed:@"slider_minimum.png"] 
    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
UIImage *maxImage = [[UIImage imageNamed:@"slider_maximum.png"] 
    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
UIImage *thumbImage = [UIImage imageNamed:@"thumb.png"];

     [[UISlider appearance] setMaximumTrackImage:maxImage 
        forState:UIControlStateNormal];
    [[UISlider appearance] setMinimumTrackImage:minImage 
        forState:UIControlStateNormal];
    [[UISlider appearance] setThumbImage:thumbImage 
        forState:UIControlStateNormal];


回答2:

Read the documentation: UISlider Class Reference

Have a close look at the following methods:

Changing the Slider’s Appearance

setMinimumTrackImage:forState:
setMaximumTrackImage:forState:
setThumbImage:forState:

For background have a look at UIView documentation.



回答3:

IF you want some example code I could recommend you to look at Apple's example code project UICatalog. This project gives you some basic knowledge about many of the UI elements. In the example they have a custom slider with different colors to the default UISlider.