且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 13:09:52

希望它会起作用...

  1. 实现这个委托方法并返回视图的高度

  • (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{返回您的视图高度;}

  1. 为标签和视图设置框架.标签的框架不超过视图的框架

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{//1.创建您的视图并为视图设置框架//2.创建标签 1 和标签 2 为标签设置框架//3.添加并返回您的视图}

I want to make picker with 2 lines, I tried it like a link , but I can not understand what I need to do: create a view and 2 label on it, when I add labels coordinates to code, but selection field still like it as default. How can I change selection field size? And text in selection field have bigger size when other lines. Sorry for my english.

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

UPDATE

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

Hope It would work...

  1. Implement this delegate method and return your view's height

  • (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return yourViewHeight; }

  1. Set frame for your label and your view..Frame of your label does not exceed the view's frame

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