ios - UIPickerView - 移除曲率并添加文本

标签 ios uipickerview uipickerviewdatasource uipickerviewdelegate

我正在尝试创建一个 UIPicker,我可以在其中选择分钟和秒。这是代码:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
        return 24;

    return 60;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30;
}

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [paragraphStyle setTailIndent:20];

    NSString *value = [NSString stringWithFormat:@"%d", row];

    return [[NSAttributedString alloc] initWithString:value
                                           attributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 160;
}

我怎样才能摆脱烦人的曲率而只制作垂直列?

另外,如何在选择行中添加“min”和“sec”?

picker

最佳答案

我发现更改组件的宽度会影响 UIPickerView 应用于所选行的偏移量或曲率效果。在您的委托(delegate)中实现 pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat 并尝试返回刚好足以显示您要显示的信息的值,例如 40 或 60一个简单数字的两个文本字符。要更改列/组件之间的间距,您可以添加返回 0 行数的额外组件,并使用这些空组件的宽度四处移动并调整要在其中显示数据的组件的位置。

关于ios - UIPickerView - 移除曲率并添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25691519/

相关文章:

ios - ios的正则表达式

ios - enumerateObjectsUsingBlock 和完成在一起

ios - 有没有办法在 iOS 中获取分钟和小时的集合/数组?

ios - Swift 中的 pickerView 不显示数据

ios - UIPickerView:NSAttributedString 在 iOS 7 中不可用?

ios - UIPickerView pickerView :titleForRow:rowforComponent method not called

c++ - Xcode 7 不使用 C++ 框架构建项目

ios - UITableView 单元格投影问题

ios - 选择 DatePicker Swift 4 的默认值