0

I'm using UISegmentController (default provided by iOS). I want to show text for iPad or iPhone landscape view. Where as in case of iPhone, I want to show icons (not text) in the same segment bar. Is it possible? If yes, then how can I achieve this in story board or programmatically?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Fayza Nawaz
  • 2,256
  • 3
  • 26
  • 61

2 Answers2

1

You can achieve this by adding UISegmentedControl programatically. Code is as follows in controller viewWillAppear method,

- (void)viewWillAppear:(BOOL)animated {
UISegmentedControl * cntrl = [[UISegmentedControl alloc]  initWithItems:@[[UIImage imageNamed:@"Circle"],@"2"]];
cntrl.frame = CGRectMake(30, 100, 200, 50);
[self.view addSubview:cntrl];
}

With above code we're creating segment control with one image in first segment and text in second segment. Check attached screenshot.

Use following code to check either device is iPad or iPhone

-(BOOL) isiPad {
return UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad;
}

Based on the type of the device, customise segment control.

For existing segment control, when you want to set image,

[cntrl setImage:[UIImage imageNamed:@"Tick"] forSegmentAtIndex:0];

use above method by specifying which image you want to set at particular segmentIndex.

At the same way when you want to set text at particular index, use

    [cntrl setTitle:@"Hello" forSegmentAtIndex:0];
AskIOS
  • 918
  • 7
  • 19
0

It doesnt seem possible to use the storyboard completely on its own to do this kind of functionality, but a combination of this and this inside your viewDidLoad might get you want you want

Community
  • 1
  • 1
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • if you dont have an option like this in the picture then you arent using size classes for your storyboard (which can be turned on in the file inspector, first tab on the top right toolbar) – Fonix Jan 15 '16 at 06:36
  • thanks, but i'm using size classes and getting the option as in above picture. the problem is, when i set image for one size class (say wAny hCompact), i appears for all. same is the case with text @Fonix – Fayza Nawaz Jan 15 '16 at 06:38
  • so do you have it like, any/any = icons, then any/compact = text and regular/regular = text ? – Fonix Jan 15 '16 at 06:41
  • i guess maybe the storyboard isnt clever enough to have different values for the different size classes then, might have to do it programmatically then unfortunately – Fonix Jan 15 '16 at 06:42
  • i have any/any = text, then compact/any=icons and then any/compact=icon.. i set in the sequence mentioned. but when i run, it's always the last thing (icons in my case) for all the size classes – Fayza Nawaz Jan 15 '16 at 06:43
  • yes, i think the same. but now i don't know how to do that programatically :/ – Fayza Nawaz Jan 15 '16 at 06:44
  • i think you need to use a combination of [this](http://stackoverflow.com/a/7696332/1219956) and [this](http://stackoverflow.com/a/10167255/1219956) – Fonix Jan 15 '16 at 06:46