Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic resizing in a navigation bar of a segmented control built dynamically

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
like image 263
ptrico Avatar asked Aug 31 '11 10:08

ptrico


1 Answers

After calling insertSegmentWithTitle call

[segmentedControl sizeToFit];

like image 106
Krzysztof Luks Avatar answered Sep 28 '22 18:09

Krzysztof Luks