开发者

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.


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];


Sure. You can use -setWidth:forSegmentAtIndex: (be sure to do it after you set the segmented control's bounds/frame), or just change the width in your interface file (it seems this doesn't work in the navigation bar, only elsewhere):

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


If you just want to adjust the size of the segment element to its contents use this BOOL property:

apportionsSegmentWidthsByContent

and set it to YES (default is NO)


iOS 6 Update

In iOS 5 and lower we must call setWidth: forSegmentAtIndex: after the frame and other display properties were set to have it work. This is corrected in iOS 6 and the order no longer matters.


Place the your setWidth:forSegmentAtIndex: call after the your `setFrame:' call. That should do the trick. Most of the time ordering doesn't matter to much in Cocoa Touch, just so long as everything is done before the end of the code. In this case, ordering does matter.

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 setWidth:45 forSegmentAtIndex:0];
        [segmentedControl setWidth:20 forSegmentAtIndex:1];
        segmentedControl.momentary = NO;
        [segmentedControl setSelectedSegmentIndex:0];

        self.navigationItem.titleView = segmentedControl;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜