【问题标题】:How to get Custom UIPickerView?如何获取自定义 UIPickerView?
【发布时间】:2014-08-06 09:40:42
【问题描述】:

我想在Custom UIPiker中实现如下功能,如下图所示。

我只想像上面那样更改选定区域的文本颜色。

【问题讨论】:

  • 将此添加到问题中,不在评论中
  • 您必须为不同的选择使用不同的标签。检查一下。

标签: ios ios7 uipicker


【解决方案1】:

在您的viewDidLoad 方法中的pickerview 中添加标签,如下所示。

ViewController.h文件中定义labelmyPickerView

- (void)viewDidLoad
{

    myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    myPickerView.delegate = self;
    myPickerView.showsSelectionIndicator = YES;
    [self.view addSubview:myPickerView];

    label = [[UILabel alloc] initWithFrame:CGRectMake(145, 76, 36, 36)];
    //label.text = @"Label";
    label.font = [UIFont boldSystemFontOfSize:20];
    label.layer.cornerRadius = 18;
    [label setTextColor:[UIColor whiteColor]];
    label.backgroundColor = [UIColor blueColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake (0,1);
    [myPickerView addSubview:label];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

在pickerview委托中设置标签标题。

#pragma mark - PickerView delegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection

    [label setText:[NSString stringWithFormat:@"%d",row]];
    NSLog(@"%@",[NSString stringWithFormat:@"%d",row]);

}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    NSLog(@"%@",[NSString stringWithFormat:@"%d",row]);
    [label setText:[NSString stringWithFormat:@"%d",row]];
    return [NSString stringWithFormat:@"%d",row];
}

你的输出是:

【讨论】:

  • 感谢它的工作原理.....只有 1 件事无法绕过标签背景的角落,虽然使用:label.layer.cornerRadius = 18;
  • 您的标签宽度和高度相同,角半径是宽度的一半。那么你的标签是四舍五入的。
  • 奇怪的行为当你向上拖动它时它会变为 4 并变回 0,即使 4 位于选择的底部
猜你喜欢
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多