Ios 如何获取自定义UIPickerView?
我想在自定义UIPiker中实现以下功能,如下图所示Ios 如何获取自定义UIPickerView?,ios,ios7,uipicker,Ios,Ios7,Uipicker,我想在自定义UIPiker中实现以下功能,如下图所示 我只想像上面那样更改所选区域的文本颜色。在您的viewdiload方法中的pickerview中添加标签,如下所示 在ViewController.h文件中定义label和myPickerView - (void)viewDidLoad { myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)]; myPickerVi
我只想像上面那样更改所选区域的文本颜色。在您的
viewdiload
方法中的pickerview中添加标签,如下所示
在ViewController.h
文件中定义label
和myPickerView
- (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];
}
您的输出是:
将此添加到问题中,而不是在评论中。您必须为不同的选择使用不同的标签。检查一下。谢谢。它能用。。。。。尽管使用:label.layer.cornerRadius=18,但有一件事无法绕过标签背景的拐角;标签的宽度和高度相同,角半径为宽度的一半。然后你的标签是圆形的。当你把它向上拖动时,它会变为4,再变回0,即使4在选择的底部