ios - UIPickerView 居中列

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

最佳答案

尝试实现以下委托(delegate)方法:

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

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

关于ios - UIPickerView 居中列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29038901/

相关文章:

ios - 如何在幻灯片中显示pdf文件?(iphone应用开发)

MATLAB 定时器在函数内部的行为与在命令窗口中的行为不同

swift - 如何从 uipickerView : titleForRow 中获取 fetchedResultsController.objectAtIndex Path()

ios - 无法将 CollectionViewCell 移动到末尾

ios - 如何在 iOS 中设置自定义日期格式?

ios - textField 的 inputAssistantItem 栏的样式

php - 定时器 mysql php 检查

c# - MyThread 和定时器控件的问题

ios - 显示 UIPickerView 时如何向上移动 View

ios - 如何使 UIPickerView 组件环绕?