我正在以编程方式将UISegmentedControl添加到UINavigationController的工具栏(我在UITableViewController中)。我希望分段控制看起来很体面,而不是填满整个酒吧。另外,我希望它随视图旋转并调整大小。这应该很容易,但我想我错过了一些东西。我实际上已经掌握了它的工作原理,但这不是干净的代码,所以我希望有人能告诉我哪些设置/方法用于“适当的”实现。UINavigationController中的UISegmentedControl UIToolbar未正确调整大小
消气:
- (UISegmentedControl*)stockFilterSegmentedControl {
if (!_stockFilterSegmentedControl) {
_stockFilterSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"All",@"Holdings", nil]];
[_stockFilterSegmentedControl addTarget:self action:@selector(stockFilterControlPressed:) forControlEvents:UIControlEventValueChanged];
_stockFilterSegmentedControl.selectedSegmentIndex = 0;
_stockFilterSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
CGRect newFrame = _stockFilterSegmentedControl.frame;
newFrame.size.height = self.navigationController.toolbar.frame.size.height * .8;
_stockFilterSegmentedControl.frame = newFrame;
}
return _stockFilterSegmentedControl;
}
当我们插入:
- (NSArray*)navFooterToolbarArray {
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.stockFilterSegmentedControl];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
return [NSArray arrayWithObjects:flexibleSpace, barButtonItem, flexibleSpace, refresh, nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Stocks";
self.toolbarItems = [self navFooterToolbarArray];
}
,并使其工作,我不得不补充:
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
NSLog(@"Did autorotate.");
CGRect newFrame = self.stockFilterSegmentedControl.frame;
newFrame.size.height = self.navigationController.toolbar.frame.size.height * .8;
self.stockFilterSegmentedControl.frame = newFrame;
}
什么是应该做的正确方法?
感谢,
达明
自iOS 7以来,它不再工作。是否有任何其他iOS 7和更高版本的解决方案? – Vinh 2014-05-30 09:02:02
确认,在iOS7中不起作用 – malex 2014-08-11 15:46:15