我使用自定义标签用于uipickerview,并使用以下代码
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 0.0f, [pickerView rowSizeForComponent:component].width - 20.0f, [pickerView rowSizeForComponent:component].height)];
pickerLabel.adjustsFontSizeToFitWidth = YES;
if (component == 0) {
pickerLabel.text = [aCAddWeightData.intValueWeights objectAtIndex:row];
pickerLabel.textAlignment= NSTextAlignmentRight;
} else {
pickerLabel.text = [aCAddWeightData.floatValueWeights objectAtIndex:row];
pickerLabel.textAlignment= NSTextAlignmentLeft;
}
return pickerLabel;
}
我明白了
我想要对齐所选的行和垂直行中的其他行,我如何能做到这一点?
发布于 2014-05-22 11:09:26
正如我所看到的,只有当我们有两个组件而没有为组件设置宽度时,才会发生这种情况。我找到了解决办法。实际上,它的工作原理还不清楚,但是,当我们将组件值的宽度设置为小于snerView.framework. set .wide/3(万一有两个组件时)时,我们会得到所选的行对齐以及未选定的行。
// implement UIPickerViewDelegate's method
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 80; // any value less than 106.6, assuming pickerView's width = 320
}
对我来说很好。
发布于 2013-12-24 21:05:50
发布于 2016-08-31 09:17:16
此解决方案适用于任何UIPickerView,无论是否自定义。
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat width = pickerView.frame.size.width;
NSUInteger count = pickerView.numberOfComponents;
//If count is wrong, use this instead:
//NSUInteger count = yourDatasource.count;
if(count > 1){
width /= count;
}
return width;
}
如果您需要对齐,请使用以下方法:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* label = (UILabel*)view;
if (!label){
label = [[UILabel alloc] init];
[label setFont:[UIFont systemFontOfSize:13.0f]];
//Set here alignment depending on your component if needed
[label setTextAlignment:NSTextAlignmentCenter];
}
[label setText:@"Your text from your datasource"];
return label;
}
注:当然是UIPickerViewDelegate
。
https://stackoverflow.com/questions/20765133
复制相似问题