The MDCSlider object is a Material Design control used to select a value from a continuous range
or discrete set of values.
- Material Design guidelines: Slider
- Class: MDCSlider
- Protocol: MDCSliderDelegate
Add the following to your Podfile:
pod 'MaterialComponents/Slider'Then, run the following command:
pod installTo import the component:
import MaterialComponents.MaterialSlider#import "MaterialSlider.h"MDCSlider can be be used like a standard UIControl.
override func viewDidLoad() {
super.viewDidLoad()
let slider = MDCSlider(frame: CGRect(x: 50, y: 50, width: 100, height: 27))
slider.addTarget(self,
action: #selector(didChangeSliderValue(senderSlider:)),
for: .valueChanged)
view.addSubview(slider)
}
func didChangeSliderValue(senderSlider:MDCSlider) {
print("Did change slider value to: %@", senderSlider.value)
}- (void)viewDidLoad {
MDCSlider *slider = [[MDCSlider alloc] initWithFrame:CGRectMake(50, 50, 100, 27)];
[slider addTarget:self
action:@selector(didChangeSliderValue:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
}
- (void)didChangeSliderValue:(MDCSlider *)slider {
NSLog(@"did change %@ value: %f", NSStringFromClass([slider class]), slider.value);
}MDCSlider exposes stateful APIs to customize the colors for different control states. In order to use this API you must enable statefulAPIEnabled on your MDCSlider instances.
let slider = MDCSlider()
slider.isStatefulAPIEnabled = true
// Setting a thumb color for selected state.
slider.setThumbColor(.red, for: .selected) MDCSlider *slider = [[MDCSlider alloc] init];
slider.statefulAPIEnabled = YES;
// Setting a thumb color for selected state.
[slider setThumbColor:[UIColor redColor] forState:UIControlStateSelected];Does not have api to:
- set right and left icons
- set the thumb image
- set the right and left track images (for a custom track)
- set the right (background track) color
Same features:
- set color for thumb via @c thumbColor
- set color of track via @c trackColor
New features:
- making the slider a snap to discrete values via property numberOfDiscreteValues
You can theme a slider with your app's color scheme using the ColorThemer extension.
You must first add the Color Themer extension to your project:
pod 'MaterialComponents/Slider+ColorThemer'// Step 1: Import the ColorThemer extension
import MaterialComponents.MaterialSlider_ColorThemer
// Step 2: Create or get a color scheme
let colorScheme = MDCSemanticColorScheme()
// Step 3: Apply the color scheme to your component
MDCSliderColorThemer.applySemanticColorScheme(colorScheme, to: component)// Step 1: Import the ColorThemer extension
#import "MaterialSlider+ColorThemer.h"
// Step 2: Create or get a color scheme
id<MDCColorScheming> colorScheme = [[MDCSemanticColorScheme alloc] init];
// Step 3: Apply the color scheme to your component
[MDCSliderColorThemer applySemanticColorScheme:colorScheme
toslider:component];