UIPickerView 居中列

UIPickerView centered columns

我正在创建一个自定义时间选择器,使用 this 问题作为指导,但我的列没有对齐。

这是我创建小时和分钟标签的地方:

UILabel *hourLabel = [[UILabel alloc] initWithFrame:CGRectMake(datePickerCell.pickerView.frame.size.width/2 - 25, datePickerCell.pickerView.frame.size.height / 2 - 15, 50, 30)];
            hourLabel.text = @"hour";
            [datePickerCell.pickerView addSubview:hourLabel];

            UILabel *minsLabel = [[UILabel alloc] initWithFrame:CGRectMake(datePickerCell.pickerView.frame.size.width - 25, datePickerCell.pickerView.frame.size.height / 2 - 15, 50, 30)];
            minsLabel.text = @"min";
            [datePickerCell.pickerView addSubview:minsLabel];

这是我的 viewForRow:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *columnView = [[UILabel alloc] initWithFrame:CGRectMake(pickerView.frame.size.width/4  - 15, 0, 35, 30)];
    columnView.text = [NSString stringWithFormat:@"%lu", row];
    columnView.textAlignment = NSTextAlignmentRight;

    return columnView;
}

如何让 0 与底部数字对齐?

尝试实现以下委托方法:

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

将宽度减少到您需要的最小值 space 它们应该排列得更好,因为这样可以防止文本向左或向右移动。还可以尝试将文本居中放置在 1 到 2 个字符之间,以强制内容保持一致。

我认为这是您分配给 pickerView 的 space 的数量,请尝试使用显示 pickerView 的列的宽度并使它们稍微大一些直到数字排成一行