UIPickerView - multi-line rows - need layout advic

2019-07-04 00:06发布

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.

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

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

3条回答
该账号已被封号
2楼-- · 2019-07-04 00:47

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
  }
查看更多
祖国的老花朵
3楼-- · 2019-07-04 00:49

Try this code

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
 return 50;
}

than after

- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
     UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:14];
lblTitle.text=[selectionList objectAtIndex:row];
}
return lblTitle;

}

查看更多
Deceive 欺骗
4楼-- · 2019-07-04 00:53

You should implement the pickerView:rowHeightForComponent: picker view delegate method and return a height tall enough for your custom view.

You should also set the height of v in your pickerView:viewForRow:forComponent:reusingView: method.

And lastly, if you are reusing a view, don't keep adding more labels. They will already be there from the reused view.

查看更多
登录 后发表回答