首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何对齐所选行与uipickerview中的其他行

如何对齐所选行与uipickerview中的其他行
EN

Stack Overflow用户
提问于 2013-12-24 18:12:27
回答 3查看 1.8K关注 0票数 3

我使用自定义标签用于uipickerview,并使用以下代码

代码语言:javascript
运行
复制
- (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;
}

我明白了

我想要对齐所选的行和垂直行中的其他行,我如何能做到这一点?

EN

回答 3

Stack Overflow用户

发布于 2014-05-22 11:09:26

正如我所看到的,只有当我们有两个组件而没有为组件设置宽度时,才会发生这种情况。我找到了解决办法。实际上,它的工作原理还不清楚,但是,当我们将组件值的宽度设置为小于snerView.framework. set .wide/3(万一有两个组件时)时,我们会得到所选的行对齐以及未选定的行。

代码语言:javascript
运行
复制
// implement UIPickerViewDelegate's method
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 80;  // any value less than 106.6, assuming pickerView's width = 320
}

对我来说很好。

票数 4
EN

Stack Overflow用户

发布于 2013-12-24 21:05:50

来自苹果文档在这里

不能在iOS 7上自定义选择器视图的选择指示符。

编辑:尝试完全删除.textAlignment属性。

票数 0
EN

Stack Overflow用户

发布于 2016-08-31 09:17:16

此解决方案适用于任何UIPickerView,无论是否自定义。

代码语言:javascript
运行
复制
- (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;
}

如果您需要对齐,请使用以下方法:

代码语言:javascript
运行
复制
- (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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20765133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档