我想显示一个UIPickerView
,其中每个组件都有一些硬编码的文本。比如"小时"one_answers"分钟"。现在,我可以通过创建标签并将其作为子视图放入picker视图来实现这一点。这适用于iOS 7&在上面
但对于iOS6,我的标签被UIPickerView
组件重叠,因此它们不可见。我如何在iOS 6中实现这种视图,其中每个组件的值都被标记到一个固定的标签上,这样用户就知道他们在选择什么。
我认为您可以通过构建一个包含picker视图和标签的自定义视图来实现这一点。
为了他人的利益,我就是这样做到的(基于Frog的建议):
- (void)setDatePickerView:(MyCustomCell *)iCell {
UIView *inputView = [[UIView alloc] init];
// Add Picker First
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.tag = 100;
int height = 30;
int offsetY = pickerView.frame.size.height / 2 - height / 2;
// Add a line on top of selection
if ([Utilities isIOS7orAbove]) {
UIImageView *topLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, offsetY - 2, pickerView.frame.size.width, 1)];
[topLineView setBackgroundColor:[UIColor grayColor]];
[pickerView addSubview:topLineView];
}
[inputView addSubview:pickerView];
// Add Minute Label
CGFloat minLabelX = [Utilities isIOS7orAbove] ? 97 : 90;
CGFloat labelYM = [Utilities isIOS7orAbove] ? 0.0 : 1.0;
CGFloat labelW = 50;
UILabel *minutesLabel = [[UILabel alloc] initWithFrame:CGRectMake(minLabelX, offsetY - labelYM, labelW, height)];
minutesLabel.text = @“Mins";
minutesLabel.font = [UIFont systemFontOfSize:kFontSize22];
minutesLabel.backgroundColor = [UIColor clearColor];
minutesLabel.textColor = [UIColor blackColor];
[inputView addSubview:minutesLabel];
// Now Add Seconds Label
CGFloat secLabelX = [Utilities isIOS7orAbove] ? 230 : 218;
UILabel *secondsLabel = [[UILabel alloc] initWithFrame:CGRectMake(secLabelX, offsetY - labelYM, labelW, height)];
secondsLabel.text = @“Secs";
secondsLabel.font = [UIFont systemFontOfSize:kFontSize22];
secondsLabel.backgroundColor = [UIColor clearColor];
secondsLabel.textColor = [UIColor blackColor];
[inputView addSubview:secondsLabel];
iCell.inputField.inputView = inputView;
}
只需使用ActionSheetPicker3.0,当我需要自定义选择器时,我会一直使用它。它与iOS6+完美配合。
以下是字符串选择器的使用方法:
NSArray *colors = [NSArray arrayWithObjects:@"Hours", @"Mins", @"Whatever", @"More", nil];
[ActionSheetStringPicker showPickerWithTitle:@"Select a Color"
rows:colors
initialSelection:0
doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
NSLog(@"Picker: %@, Index: %@, value: %@",
picker, selectedIndex, selectedValue);
}
cancelBlock:^(ActionSheetStringPicker *picker) {
NSLog(@"Block Picker Canceled");
}
origin:sender];
享受吧!