2

I have a custom UISegmentedControl with 2 sections and it is declared programmatically so I cannot use IB. I set a background image, tint, and a title. I need to have an image display in line with the title. If i set the image, the title disappears and vice versa.

I need a way to display both an image and the title in line on the segment. Any thoughts?

JeffN
  • 1,575
  • 15
  • 26
  • Plz refer this link..hope it works perfect.. http://stackoverflow.com/questions/13457876/uisegmentedcontrol-with-image-and-title – Vinay Podili Jul 31 '15 at 10:11

2 Answers2

0

Creating a subclass of UISegmentedControl, overwriting

[MySegmentedControl initWithItems:] 
-(id) initWithItems:(NSArray*)items {
    [self setImage:anImage forState:aState barMetrics:UIBarMetricsDefault];
    [self setTitleTextAttributes:aDict forState:aState];
    [self setDividerImage:anImage forLeftSegmentState:aState rightSegmentState:aState barMetrics:UIBarMetricsDefault];
    ... do more UI stuff ...
}

And in your view controller, you call it with:

-(void)viewDidLoad {
    [self.view addSubview:[MySegmentedControl alloc] initWithItems:@[@"one",@"two"]]];
    ...

works for me reliably for iOS6/7.

nine stones
  • 3,264
  • 1
  • 24
  • 36
-2

Assuming segmentedControl is in your .h as

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

Do this in your .m for your viewController and set up the segments in code

[self.segmentedControl insertSegmentWithTitle:@"Title with Image" atIndex:0 animated:YES];
        [self.segmentedControl setImage:[UIImage imageNamed:@"whatever.png"] forSegmentAtIndex:0];
wm_j_ray
  • 144
  • 7
  • No, it does not work. I was in error. You may want to try this one on github -> [https://github.com/u10int/URBSegmentedControl – wm_j_ray Jul 18 '13 at 12:16