我有一个带有2个项目的UISegmentedControl。
有没有办法让左项比右项更大?
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;
不起作用。
答案 0 :(得分:12)
是的,你可以这样做: -
[*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;
现在添加这两行然后它将起作用
[segmentedControl setWidth:45 forSegmentAtIndex:0];
[segmentedControl setWidth:20 forSegmentAtIndex:1];
答案 1 :(得分:9)
不确定。您可以使用-setWidth:forSegmentAtIndex:
(确保在设置分段控件的边界/框架后执行此操作),或者只更改接口文件中的宽度(似乎这在导航栏中不起作用,仅在其他地方) :
答案 2 :(得分:5)
如果您只想将segment元素的大小调整为其内容,请使用此BOOL属性:
apportionsSegmentWidthsByContent
并将其设置为YES(默认为NO)
答案 3 :(得分:4)
在iOS 5及更低版本中,我们必须在框架和其他显示属性设置为工作后调用setWidth: forSegmentAtIndex:
。这在iOS 6中已得到纠正,订单不再重要。
答案 4 :(得分:2)
在您的`setFrame:'调用之后放置setWidth:forSegmentAtIndex:
来电。这应该够了吧。大多数时候订购在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;