Ios UISegmentControl以编程方式分段框架
我以以下方式使用段控件Ios UISegmentControl以编程方式分段框架,ios,objective-c,uisegmentedcontrol,Ios,Objective C,Uisegmentedcontrol,我以以下方式使用段控件 CGFloat segmentWidth = 70; CGFloat segmentHeight = 40; NSMutableArray *arr = [[NSMutableArray alloc] init]; for (HCIPGRentDetail *object in self.pgHouse.rents) { [arr addObject:object.roomTypeId]; } UISegme
CGFloat segmentWidth = 70;
CGFloat segmentHeight = 40;
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (HCIPGRentDetail *object in self.pgHouse.rents) {
[arr addObject:object.roomTypeId];
}
UISegmentedControl *sharingSegmentControl = [[UISegmentedControl alloc] initWithItems:arr];
[sharingSegmentControl setApportionsSegmentWidthsByContent:YES];
sharingSegmentControl.frame = CGRectMake(0, 0, self.pgHouse.rents.count * segmentWidth, segmentHeight);
for (int i = 0; i < sharingSegmentControl.numberOfSegments; i++) {
[sharingSegmentControl setWidth:segmentWidth forSegmentAtIndex:i];
[sharingSegmentControl setTitle:arr[i] forSegmentAtIndex:i];
}
sharingSegmentControl.center = CGPointMake(self.view.center.x, currentHeight + sharingSegmentControl.frame.size.height/2);
currentHeight += sharingSegmentControl.frame.size.height;
[sharingSegmentControl.layer setBorderColor:[UIColor redColor].CGColor];
[sharingSegmentControl.layer setBorderWidth:1.0f];
[sharingSegmentControl setSelectedSegmentIndex:0];
[sharingSegmentControl setBackgroundColor:[UIColor blueColor]];
[sharingSegmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.mainScrollView addSubview:sharingSegmentControl];
CGFloat段宽度=70;
CGFloat段高度=40;
NSMutableArray*arr=[[NSMutableArray alloc]init];
for(self.pgHouse.rents中的HCIPGRentDetail*对象){
[arr addObject:object.roomTypeId];
}
UISegmentedControl*sharingSegmentControl=[[UISegmentedControl alloc]initWithItems:arr];
[sharingSegmentControl SetArrightsSegmentWidthsByContent:是];
sharingSegmentControl.frame=CGRectMake(0,0,self.pgHouse.rents.count*segmentWidth,segmentHeight);
对于(int i=0;i
它只显示带有蓝色背景和红色的段控件,这些段得到一个类似{0,0},{0,29}的帧(调试器)。不明白我错过了什么
调试器这样说
<__NSArrayM 0x1411be30>(
<UISegment: 0x1411bd60; frame = (0 0; 0 29); opaque = NO; layer = <CALayer: 0x1411bd30>>,
<UISegment: 0x1411a1c0; frame = (0 0; 0 29); opaque = NO; layer = <CALayer: 0x1411b220>>
)
(
,
)
我复制了您的代码并对其进行了一点编辑,因此这对我很有用:
CGFloat segmentWidth = 70;
CGFloat segmentHeight = 40;
UISegmentedControl *sharingSegmentControl = [[UISegmentedControl alloc] initWithItems:@[@"AAA", @"BBB"]];
//[sharingSegmentControl setApportionsSegmentWidthsByContent:YES];
sharingSegmentControl.frame = CGRectMake(0, 0, sharingSegmentControl.numberOfSegments * segmentWidth, segmentHeight);
for (int i = 0; i < sharingSegmentControl.numberOfSegments; i++) {
[sharingSegmentControl setWidth:segmentWidth forSegmentAtIndex:i];
//[sharingSegmentControl setTitle:arr[i] forSegmentAtIndex:i];
}
[sharingSegmentControl.layer setBorderColor:[UIColor redColor].CGColor];
[sharingSegmentControl.layer setBorderWidth:1.0f];
[sharingSegmentControl setSelectedSegmentIndex:0];
[sharingSegmentControl setBackgroundColor:[UIColor blueColor]];
[sharingSegmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.view addSubview:sharingSegmentControl];
CGFloat段宽度=70;
CGFloat段高度=40;
UISegmentedControl*sharingSegmentControl=[[UISegmentedControl alloc]initWithItems:@[@“AAA”,“BBB”];
//[sharingSegmentControl SetArrightsSegmentWidthsByContent:是];
sharingSegmentControl.frame=CGRectMake(0,0,sharingSegmentControl.numberOfSegments*segmentWidth,segmentHeight);
对于(int i=0;i
结果表明,色调是问题所在。可能段控制将色调默认值作为清晰颜色或其他颜色
不知怎的,当我刚设置色调时。我获得了正确的帧,显示正确。您的对象是否包含值?是的,它包含值。标题也设置正确。问题在于中心,在指定帧后设置中心。”中心在其superview的坐标系内指定,并以点为单位进行测量。设置此属性会相应地更改帧属性的值“@pawan并不能真正解释为什么将段(所有段)的宽度设置为0.No。这对我不起作用。检查我编辑的问题。