I have my own custom UISegmentedControl subclass. It’s buttons are bigger than the standard size. Each button is 100.0 points wide.
@implementation SegmentedControlExplore
- (id) init
{
return [super
initWithItems:[NSArray arrayWithObjects:
@"Uno",
@"Dos",
nil
]
];
}
- (void) layoutSubviews
{
for (NSUInteger i = 0; i < self.numberOfSegments; i++) {
[self setWidth:100.0 forSegmentAtIndex:i];
}
[super layoutSubviews];
}
@end
When I attempt to use this in my controller, I could not center it in my UINavigationBar.
SegmentedControl* sc = [[[SegmentedControl alloc] init] autorelease];
[self.navigationController.navigationBar addSubview:sc];
sc.center = self.navigationController.navigationBar.center;
It’s off-center to the right. The reason is because the button size setting actually gets called after its center property gets changed. So where should I move my button size setting so it gets done first? I tried moving it to init, but the size did not get changed.
Replace your
layoutSubviewsfunction with this code.