Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell with centered UISlider and two images

I'd like to programmatically create a custom UITableViewCell with a UISlider in the center and two images at either end. For an example see the Brightness setting on any iOS device. It has a UISlider in the center and two sun-like images at either end (one small and one large). Here's basically what I'm talking about:

enter image description here

What's the easiest way to programmatically create this custom UITableViewCell?

Cheers!

like image 688
MrDatabase Avatar asked Dec 10 '22 07:12

MrDatabase


1 Answers

The easiest way to add the images at either end of a UISlider is to use setMinimumTrackImage:forState: and setMaximumTrackImage:forState:.

To add a UISlider to a UITableViewCell, just add it as a subview of the cell's contentView, and set the slider's frame (or bounds and center) and autoresizingMask as appropriate.

    [cell.contentView addSubview:slider];
    slider.bounds = CGRectMake(0, 0, cell.contentView.bounds.size.width - 10, slider.bounds.size.height);
    slider.center = CGPointMake(CGRectGetMidX(cell.contentView.bounds), CGRectGetMidY(cell.contentView.bounds));
    slider.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

Ignore the cell's imageView, textLabel, and detailTextLabel properties; if you never access these properties, the cell may not even bother to create the corresponding views.

like image 109
Anomie Avatar answered Mar 08 '23 23:03

Anomie