Jessica
Jessica

Reputation: 9830

Apply auto-layout to UISegmentedControl

I set custom widths for segments in UISegmentedControl. Because of that, the SegmentedControl doesn't use auto-layout. (In the storyboard, I applied a constraint on all 4 sides of the SegmentedControl.)

Here is my code:

CGFloat segmentWidth = self.segment.frame.size.width;

[self.segment insertSegmentWithTitle:@"titleName" atIndex:2 animated:NO];    
[self.segment setWidth:segmentWidth / 6 forSegmentAtIndex:1];
[self.segment setWidth:segmentWidth / 6 forSegmentAtIndex:2];

[self.segment setWidth:segmentWidth / 3 forSegmentAtIndex:0];

Here's an image to illustrate what this code does:

enter image description here

Is there a way to add auto-layout to the segments?

Upvotes: 1

Views: 1623

Answers (1)

matt
matt

Reputation: 535118

The problem is that your code is in the wrong place. Put it in viewDidLayoutSubviews and all will be well.

Upvotes: 2

Related Questions