如何只用3个可见行制作uipickerview?像这样:
答案 0 :(得分:2)
您必须以编程方式添加UIDatePicker才能显示三行。根据需要调整拾取器尺寸。
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.frame = CGRectMake(0, 160, 320, 160);
datePicker.datePickerMode=UIDatePickerModeDate;
[self.view addSubview:datePicker];
答案 1 :(得分:2)
我注意到大多数回复都提到 UIDatePicker 或组件的数量,这两个似乎都没有,因为问题专门询问 UIPickerView 和行可见的数量。
我通过实施找到了解决方案:
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return (80.0);
}
这允许您使行的高度更大(或更小),以便控件中显示更少(或更多)的行,尽管控件的垂直大小相同。
我确实可以改变控件的大小以避免所有多余的间距,但我还没有解决这个问题。
行高25.0
行高80.0
答案 2 :(得分:2)
除了@ GRW的回答。因此,您无法将任意高度设置为选择器视图;但你可以:改变行高度有3个可见行;并应用仿射变换来缩小整个选择器视图。
保持宽高比:
// consider CGFloat _scaleFactor = 0.5; - (void)viewDidLoad { [super viewDidLoad]; self.pickerView.transform = CGAffineTransformMakeScale(_scaleFactor, _scaleFactor); } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return desiredHeight / _scaleFactor; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component; { return desiredWidth / _scaleFactor; } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return desiredHeight / _scaleFactor; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *label = (UILabel *)view; if (!label) { label = [[UILabel alloc] init]; CGFloat desiredFontSize; label.font = [UIFont systemFontOfSize:(desiredFontSize / _scaleFactor)]; } // set text from data source return label; }
阻止不可见行的差异非常微妙;但是,在我看来,右侧的缩放选择器看起来更自然......事实上,这会产生很大的不同。
(使用的比例因子为0.5)
答案 3 :(得分:0)
尝试以下代码 -
#pragma mark - picker datasource methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 3;
}
#pragma mark - picker delegate
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [YourArray objectAtIndex:row];//your array that contains data
}
答案 4 :(得分:0)
请尝试以下代码:
//Method to define how many columns/dials to show
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
// Method to define the numberOfRows in a component using the array.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component
{
if (component==0)
{
return [arrFirst count];
}
else if (component==1)
{
return [arrSecond count];
}
else
{
return [arrThird count];
}
}
// Method to show the title of row for a component.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component)
{
case 0:
return [arrFirst objectAtIndex:row];
break;
case 1:
return [arrSecond objectAtIndex:row];
break;
case 2:
return [arrThird objectAtIndex:row];
break;
}
return nil;
}
谢谢,如果您有任何问题,请告诉我。