Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Icon AND Text in UISegmentedController

A segment can only have an image or a title; it can’t have both. There is no default image.

So says Apple in the UISegmentedController Reference.

Has anyone come up with a generalized solution that allows you to have both an icon and text? I have a Segmented Control that has four segments. There are options for customizing the appearance of the control, but they only seem designed for a UISegmentedControl with two segments?

Ideas I'm kicking around:

  1. Ditch the segmented control for four UIButtons and handle the "selected" state myself
  2. Throw my hands up just go with text or icons.
  3. Your suggestions...?
like image 778
Ben Jacobs Avatar asked Sep 04 '12 21:09

Ben Jacobs


2 Answers

You create a new image by drawing your current image into a context, draw the text you want,then use that combo image as the single segment image:

UIGraphicsBeginImageContextWithOptions(size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Text first
[myString drawAtPoint:somePoint withFont:font];

// Images upside down so flip them 
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ imagePoint, imageSize }, [myUIimage CGImage]);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
like image 153
David H Avatar answered Oct 15 '22 08:10

David H


You may want to try a third-party alternative such as STSegmentedControl (No relation, but I've used it, and it sounds like it will give you the flexibility you need without starting from scratch).

like image 20
Mike Fahy Avatar answered Oct 15 '22 09:10

Mike Fahy