Icon and text in UISegmentedController

A segment can only have an image or title; he cannot have both. There is no default image.

This is what Apple says 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 adjusting the appearance of the control, but they seem designed only for a UISegmentedControl with two segments?

Ideas that I kick:

  • Rotate the segmented control for the four UIButtons and handle the β€œselected” state yourself.
  • Throw my hands, just go with text or icons.
  • Your suggestions...?
+8
ios iphone cocoa-touch
source share
2 answers

You can try a third-party alternative such as STSegmentedControl (No relationship, but I "He used it, and it looks like it will give you the necessary flexibility without starting from scratch."

+5
source share

You create a new image by drawing your current image in context, draw the text you want, then use this combined image as an image of one segment:

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; 
+10
source share

All Articles