UIPickerView - multi-line rows - need layout advice

南笙酒味 提交于 2019-12-14 03:53:50

问题


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


回答1:


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
  }



回答2:


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.




回答3:


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;

}



来源:https://stackoverflow.com/questions/20510969/uipickerview-multi-line-rows-need-layout-advice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!