ios - UIPickerView - 多行行 - 需要布局建议

标签 ios uipickerview

我想用 2 行制作选择器,我试过 a link ,但我不明白我需要做什么:当我将标签坐标添加到代码时,在其上创建一个 View 和 2 个标签,但选择字段仍然像默认值一样。如何更改选择字段的大小?并且选择字段中的文本在其他行时具有更大的尺寸。对不起我的英语。

enter image description here

- (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;
}

http://i.picresize.com/images/2013/12/11/IiYqd.png

最佳答案

希望它能奏效...

  1. 实现这个委托(delegate)方法并返回 View 的高度
  • (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return yourViewHeight; }
  1. 为标签和 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/

相关文章:

ios - 如何使 UIAction 按钮在越界时弹回

ios - UIAlertView 具有多个文本输入?

arrays - 使用重量或长度数据填充 pickerView

json - 在 UIPickerView 上选择一行时如何用信息填充第二个 View Controller

iphone - NSMutableArray 到 NSString 的转换提供数组而不是字符串

ios - 如何在 didSelectRowAt indexPath 中选择正确的实体

iphone - UIScrollView 未正确更新和显示

swift - 3 秒后没有反应淡出日期选择器

iphone - 从第二个 View 中获取所选项目

ios - UIPickerView 行在滚动期间消失