如何获得自定义UIPickerView [英] how to get Custom UIPickerView

查看:148
本文介绍了如何获得自定义UIPickerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Guys请帮助我,我想在Custom UIPiker中实现以下功能如下图所示。

Hey Guys Please help me i want to achieve the following functionality in Custom UIPiker As shown in below Picture.

我想仅像上面那样更改所选区域的文字颜色

I want to change the text colour of selected area only like above

推荐答案

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

Add lable in your pickerview in your viewDidLoad method as below.

中定义标签 myPickerView ViewController.h file

Define label and myPickerView both in ViewController.h file

- (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委托设置标签标题中。

in pickerview delegate set label title.

#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];
}

您的OuytPut是:

这篇关于如何获得自定义UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆