有没有办法让UISegmentedControl按钮的大小不同?

时间:2022-09-06 21:16:32

I have a UISegmentedControl with 2 items.

我有一个带有2个项目的UISegmentedControl。

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.

不起作用。

5 个解决方案

#1


12  

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

#2


9  

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):

当然。您可以使用-setWidth:forSegmentAtIndex :(确保在设置分段控件的边界/框架后执行此操作),或者只更改接口文件中的宽度(似乎这在导航栏中不起作用,仅在其他地方) :

有没有办法让UISegmentedControl按钮的大小不同?

#3


5  

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

如果您只想将segment元素的大小调整为其内容,请使用以下BOOL属性:

apportionsSegmentWidthsByContent

apportionsSegmentWidthsByContent

and set it to YES (default is NO)

并将其设置为YES(默认为NO)

#4


4  

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.

在iOS 5及更低版本中,我们必须调用setWidth:forSegmentAtIndex:在将帧和其他显示属性设置为使其工作之后。这在iOS 6中已得到纠正,订单不再重要。

#5


2  

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.

在你的`setFrame:'调用之后放置你的setWidth:forSegmentAtIndex:call。这应该够了吧。大多数时候订购在Cocoa Touch中并不重要,只要一切都在代码结束之前完成。在这种情况下,订购确实很重要。

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;

#1


12  

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

#2


9  

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):

当然。您可以使用-setWidth:forSegmentAtIndex :(确保在设置分段控件的边界/框架后执行此操作),或者只更改接口文件中的宽度(似乎这在导航栏中不起作用,仅在其他地方) :

有没有办法让UISegmentedControl按钮的大小不同?

#3


5  

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

如果您只想将segment元素的大小调整为其内容,请使用以下BOOL属性:

apportionsSegmentWidthsByContent

apportionsSegmentWidthsByContent

and set it to YES (default is NO)

并将其设置为YES(默认为NO)

#4


4  

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.

在iOS 5及更低版本中,我们必须调用setWidth:forSegmentAtIndex:在将帧和其他显示属性设置为使其工作之后。这在iOS 6中已得到纠正,订单不再重要。

#5


2  

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.

在你的`setFrame:'调用之后放置你的setWidth:forSegmentAtIndex:call。这应该够了吧。大多数时候订购在Cocoa Touch中并不重要,只要一切都在代码结束之前完成。在这种情况下,订购确实很重要。

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;