Turns out that tint colour was the problem. Maybe segment control take the tint colour default as clear colour or so.
Somehow, when I just set the tint colour. I got the frame correct, the display correct.
Pergunta
I'm using a segment control in the following manner.
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];
It just displays the segment control with blue colour background and red colour, the segments get a frame (debugger) like {{0,0},{0,29}}. Doesn't understand what I'm missing.
The debugger says this
<__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>>
)
Solução 2
Turns out that tint colour was the problem. Maybe segment control take the tint colour default as clear colour or so.
Somehow, when I just set the tint colour. I got the frame correct, the display correct.
Outras dicas
I copied your code and edited it just a bit, so this works for me :
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];