UIPickerView居中的列

时间:2015-03-13 17:57:42

标签: ios timer uipickerview

我正在创建一个自定义时间选择器,使用this问题作为指导,我的专栏不排成一行。

enter image description here

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

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与底部数字对齐?

2 个答案:

答案 0 :(得分:3)

尝试实现以下委托方法:

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

将宽度减小到所需的最小空间,它们应该排得更好,因为这样可以防止它向左或向右移动文本。同时尝试将文本居中于1到2个字符的位置,以强制事物保持一致。

答案 1 :(得分:0)

我认为你分配给pickerView的空间量,尝试使用显示pickerView的列的宽度,并使它们稍微大一点,直到数字排列