Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a UISegmentedControl button different sizes?

I have a UISegmentedControl with 2 items.

Is there a way to make the left item a bit larger than the right item?

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:symbol,@"+", nil]];
        [segmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
        [segmentedControl setWidth:45 forSegmentAtIndex:0];
        [segmentedControl setWidth:20 forSegmentAtIndex:1];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        segmentedControl.frame = CGRectMake(0,0,300,30);
        segmentedControl.momentary = NO;
        [segmentedControl setSelectedSegmentIndex:0];

        self.navigationItem.titleView = segmentedControl;

Doesn't work.

like image 515
Sheehan Alam Avatar asked Aug 23 '11 07:08

Sheehan Alam


1 Answers

yes you can do this:-

    [*yoursegmentcontrolobjectname* setWidth:45 forSegmentAtIndex:0];
    [*yoursegmentcontrolobjectname* setWidth:15 forSegmentAtIndex:1];   

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:symbol,@"+", nil]];
        [segmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        segmentedControl.frame = CGRectMake(0,0,300,30);
        segmentedControl.momentary = NO;
        [segmentedControl setSelectedSegmentIndex:0];

        self.navigationItem.titleView = segmentedControl;

add these two lines now then it will work

        [segmentedControl setWidth:45 forSegmentAtIndex:0];
        [segmentedControl setWidth:20 forSegmentAtIndex:1];
like image 147
Gypsa Avatar answered Sep 19 '22 13:09

Gypsa