控制器将UISegmentedControl添加到导航栏.分段控件添加到控制器的viewDidLoad方法的导航栏中,但实际的段是在调用viewDidLoad后动态创建的.
显示视图时,我无法自动调整分段大小.他们都被挤压,like in this post,虽然这里的决议不适用.如果在将分段控件添加到导航栏的右侧项目之前添加了段(打破了代码的动态特性),则会自动调整它们的大小并在显示视图时看起来很好.
这是我的代码的精简版本,如下所示.我错过了什么?
@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