A controller adds a UISegmentedControl
to a navigation bar. The segmented control is added to the navigation bar in the viewDidLoad
method of the controller but the actual segments are created dynamically after viewDidLoad
is called.
I can't get the segments to be resized automatically when the view is shown. They are all squished, like in this post, though the resolution doesn't apply here. If the segments are added before the segmented control is added to the right item of the navigation bar (breaking the dynamic nature of the code), they are resized automatically and look fine when the view is shown.
Here's a stripped down version of my code, below. What am I missing?
@implementation MyController
- (void)viewDidLoad {
// segmentedControl is an ivar
segmentedControl = [UISegmentedControl alloc] initWithItems:[NSArray array]];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
self.navigationController.navigationBar.topItem.rightBarButtonItem = barButtonItem;
}
- (void)someMethodCalledAfterViewDidLoad {
[segmentedControl insertSegmentWithTitle:@"a title"
atIndex:0
animated:NO];
}
@end
After calling insertSegmentWithTitle call
[segmentedControl sizeToFit];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With