UIPickerView – 多行行 – 需要布局建议
我想用2行做拾取器,我尝试了它就像一个链接 ,但我无法理解我需要做什么:创建一个视图和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; }
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
希望它会起作用……
- 实现此委托方法并返回视图的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)组件{return yourViewHeight; }
- 为您的标签和视图设置框架。标签的框架不会超出视图的框架
- (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 }
您应该实现pickerView:rowHeightForComponent:
视图委托方法,并为自定义视图返回足够高的高度。
您还应该在pickerView:viewForRow:forComponent:reusingView: method
设置v
的高度pickerView:viewForRow:forComponent:reusingView: method
。
最后,如果您重复使用视图,请不要继续添加更多标签。 他们已经从重用的视图出现了。
试试这个代码
- (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;
}
- 由于未捕获exception“UIViewControllerHierarchyInconsistency”而终止应用程序,
- 在Xcode 10中进行性能分析
- 在3d空间中在x轴上旋转平面时,如何计算底边的高度?
- 如何在Swift中设置UIButton背景色forState:UIControlState.Highlighted
- Swift不支持SDK'iPhoneSimulator8.4.sdk'
- UITableViewController与自定义的UITableViewCell
- 对象在ARC模式下解除分配
- 添加Google转换跟踪SDK iOS-3.0.0时,应用程序崩溃
- 关于新Snapchat更新的个人意见