0

My code so far;

    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
    [segment insertSegmentWithTitle:@"OPEN THE DOOR" atIndex:0 animated:NO];

I have 3 segments in the segment controller, open the door,no & . . I need to adjust the width of each segment based on the its title.

for example open the door requires more width and no requires a little. the . requires very little that the other 2 segments.

I tried [segment setWidth:200 forSegmentAtIndex:0]; to increase the width but it didn't work. How can i do this ?

4
  • Are you setting some action also on segment? Commented Dec 28, 2011 at 16:16
  • Yes [segment addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged]; Commented Dec 28, 2011 at 16:22
  • 1
    I don't see how that's germane to his question about the widths, even though I agree he does need to set an action. Commented Dec 28, 2011 at 16:22
  • try setting the width as the last statement, there is similar problem sometime back and that works for me. There are some resetting issue here. Just try with this and let me know if that works for you :) Commented Dec 28, 2011 at 16:29

1 Answer 1

2

If you are using iOS 5 try using the apportionsSegmentWidthsByContent property so simply set

segment.apportionsSegmentWidthsByContent = YES;

(and get rid of your existing code to set widths)

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, i'm using iOS 5 let me try it out
After you try it out, I am just curious, why is "." the title of a segment? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.