我想用 2 行制作选择器,我试过 a link ,但我不明白我需要做什么:当我将标签坐标添加到代码时,在其上创建一个 View 和 2 个标签,但选择字段仍然像默认值一样。如何更改选择字段的大小?并且选择字段中的文本在其他行时具有更大的尺寸。对不起我的英语。
- (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView* v;
if (view)
v = view;
else
{
v = [[UIView alloc] init] ;
UILabel* l1 = [[UILabel alloc] init];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] init];
l2.tag = 12;
l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[v addSubview: l2];
}
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: @"row %d line 1", row];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: @"row %d line 2", row];
l2.tag = 12;
[v addSubview: l2];
return v;
}
更新
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return yourViewHeight;
}
- (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIView* v;
if (view)
v = view;
else
{
v = [[UIView alloc] initWithFrame:CGRectMake(0, 34, 110, 35)] ;
UILabel* l1 = [[UILabel alloc] init];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] init];
l2.tag = 12;
l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[v addSubview: l2];
}
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: @"row %d line 1", row];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: @"row %d line 2", row];
l2.tag = 12;
[v addSubview: l2];
return v;
}
最佳答案
希望它能奏效...
- 实现这个委托(delegate)方法并返回 View 的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return yourViewHeight; }
- 为标签和 View 设置框架..标签的框架不超过 View 的框架
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ //1. Create your view and set frame for the view //2. Create your label 1 and label 2 set the frame for your labels //3. Add and return your view }
关于ios - UIPickerView - 多行行 - 需要布局建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20510969/