Iphone 如何更改UIPickerView中选定行的颜色

Iphone 如何更改UIPickerView中选定行的颜色,iphone,uipickerview,Iphone,Uipickerview,好吧,也许我遗漏了一些非常简单的东西,如果是这样的话,我很抱歉,但是,我已经在谷歌上搜索了标题的每个排列,没有找到!所以这就是我想要做的:当选择行时,更改我在2组件pickerview中用作行视图的标签的背景色。所以我认为这会起作用: if (row == [pickerview selectedRowForComponent]) viewlabel.backgroundColor = redcolor; 但这不起作用。它似乎随意选择要着色的行,有时甚至会给出一个糟糕的访问错误。我试过

好吧,也许我遗漏了一些非常简单的东西,如果是这样的话,我很抱歉,但是,我已经在谷歌上搜索了标题的每个排列,没有找到!所以这就是我想要做的:当选择行时,更改我在2组件pickerview中用作行视图的标签的背景色。所以我认为这会起作用:

if (row == [pickerview selectedRowForComponent])
    viewlabel.backgroundColor = redcolor;
但这不起作用。它似乎随意选择要着色的行,有时甚至会给出一个糟糕的访问错误。我试过所有不同的条款都没有效果!如有任何建议,将不胜感激

以下是完整的方法:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{

    if (component == kNumberComponent) {


#define PICKER_LABEL_FONT_SIZE 24
#define PICKER_LABEL_ALPHA 1.0
        // UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE];
        UIFont *font = [ UIFont fontWithName:@"AppleGothic"  size:24];
        UILabel *carsLabel =[ [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 50) ]autorelease];
        //[picker selectRow:row inComponent:component animated:YES];
        NSString *pickerText = [self.numbers objectAtIndex:(int)row];
        carsLabel.text = pickerText;
        carsLabel.textAlignment = UITextAlignmentCenter;
        NSLog(@"carsLabel = %@",carsLabel.text);
        //carsLabel.text = @"maybe because the string isn't long enough";
        carsLabel.textColor = [UIColor blackColor];
        carsLabel.font = font;





        carsLabel.opaque = YES;


        [view addSubview:carsLabel];

        return carsLabel;   
    } else {
        UIFont *font = [ UIFont fontWithName:@"AppleGothic"  size:18];

        UILabel *carsLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 225, 50)] autorelease];
        id fact = [self.facts objectAtIndex:(int)row];
        NSString *pickerText = @"Dictionary Entry";
        if ( [fact isKindOfClass:[NSString class]]) {


            pickerText = [self.facts objectAtIndex:(int)row];

        } 
        carsLabel.text = pickerText;
        carsLabel.textAlignment = UITextAlignmentCenter;
        NSLog(@"carsLabel = %@",carsLabel.text);
        //carsLabel.text = @"maybe because the string isn't long enough";
        carsLabel.textColor = [UIColor blackColor];
        carsLabel.font = font;
        if ( row == 0) {
        carsLabel.backgroundColor = [UIColor redColor];

        }
        //carsLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blackboard.png"]];;
        carsLabel.opaque = YES;

        [view addSubview:carsLabel];

        return carsLabel;
    }


    return nil;
}

不清楚您将上述代码放在何处。是否在-pickerView:viewForRow:forComponent:reusingView:?这就是它应该在的地方。您确定要维护指向该特定视图标签的指针吗?你正在崩溃的事实表明你可能不是。更大的代码块(包括您放置它的位置)会有所帮助。

调用UIPickerView实例的-viewForRow:forComponent:方法来获取UIView*对象。然后设置该视图的背景颜色

解决了!声明两个实例变量:selectedView和oldView。然后,下面的代码将执行此操作:

if (self.oldView != nil)
        self.oldView.backgroundColor = [UIColor clearColor];

self.selectedView = [picker viewForRow:row forComponent:kNumberComponent];
        self.selectedView.backgroundColor = [UIColor redColor];
        [self.selectedView setNeedsDisplay];
        self.oldView = self.selectedView;

viewForPicker的正确格式为:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel*) view;
    if (label == nil)
    {
        label = [[UILabel alloc] init];
    }

    [label setText:@"Whatever"];

    // This part just colorizes everything, since you asked about that.

    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:[UIColor blackColor]];
    CGSize rowSize = [pickerView rowSizeForComponent:component];
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
    [label setFrame:labelRect];

    return label;
}

上面代码的问题是:它为标签着色,而不是选择器本身。因此,当你滚动到一端或另一端时,有一个空白点,你可以看到白色背景。设置[myPicker setBackgroundColor…]并没有达到人们所希望的效果。

//cRectMake为我们想要的帧生成值

UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height)];

// add the UIPickerView to your viewcontroller [mainView addSubview:myPickerView];

// set the selectionindicator to none myPickerView.showsSelectionIndicator = NO;
//定义我们要使用的图像

UIImage *selectorImage = [UIImage imageNamed:@"selectionIndicator.png"];

UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
//将x和y值设置为UIPickerView上要放置图像的点

//将宽度和高度值设置为图像的宽度和高度

customSelector.frame = CGRectMake(10,(myPickerView.bounds.size.height / 2) + 16, self.view.bounds.size.width – 10, 47);
//将自定义selectionIndicator也添加到同一viewcontroller

[mainView addSubview:customSelector];

通常,我使用这种方法:

我使用自定义视图来显示行项目

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{

    UILabel *label = (id)view;

    if (!label)
    {

        label= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.text = _arrayStringPicker[row];

    }  

    return label;
我使用以下命令更改选定行的颜色:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
    [labelSelected setTextColor:[UIColor redColor]];

}
迅速实施

extension PickerViewController: UIPickerViewDelegate {
    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        var color: UIColor!
        if pickerView.selectedRowInComponent(component) == row {
            color = UIColor.redColor()
        } else {
            color = UIColor.blackColor()
        }

        let attributes: [String: AnyObject] = [
            NSForegroundColorAttributeName: color,
            NSFontAttributeName: UIFont.systemFontOfSize(15)
        ]

        return NSAttributedString(string: rows[row], attributes: attributes)
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //this will trigger attributedTitleForRow-method to be called
        pickerView.reloadAllComponents()
    }
}

以下是对我有效的方法:

将UIView作为子视图添加到UIPickerView 使用UIPickerView将其约束为Y输入,并且具有相同的宽度 将其高度设置为在pickerView\upickerview:UIPickerView,rowHeightForComponent:Int->CGFloat中返回的高度 使其半透明,并按您想要的方式着色
*其他具有行自定义视图或行属性字符串自定义视图的解决方案在快速滚动时出现问题。

UIPickerView具有为行标题和组件设置NSAttributeString的委托。我们可以如下设置属性颜色:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSDictionary *attrs = @{NSForegroundColorAttributeName : [UIColor redColor]};
    NSString *title = @"title"
    return [[NSAttributedString alloc] initWithString:title attributes:attrs];

}

敏捷版@alessandro pirovano答案

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    let rowSize = pickerView.rowSize(forComponent: component)
    let width = rowSize.width
    let height = rowSize.height
    let frame = CGRect(x: 0, y: 0, width: width, height: height)
    let label = UILabel(frame: frame)
    label.textAlignment = .center
    label.text = rows[row]

    return label
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    guard let label = pickerView.view(forRow: row, forComponent: component) as? UILabel else {
        return
    }
    label.backgroundColor = .orange
}

扩大亚历山德罗·皮罗瓦诺的回答

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
    [labelSelected setTextColor:[UIColor redColor]];
    labelSelected.superview.backgroundColor = [UIColor yellowColor];
}

罗布,我已经发布了完整的方法,再次感谢你的建议!我认为您误解了此委托方法的工作原理。pickerView为您提供了一个重用视图。您正在使用-addSubView:对其进行修改,但返回的是另一个视图UILabel。在这里,您应该做的第一件事是摆脱对-addSubView的调用:。我相信我已经尝试过了,但我给了它另一个目标,以确保您已将包含UIPickerView的UIView设置为pickerview的委托,并且您已指定UIView将实现UIPickerViewDeleteGate契约。嗨,Alex,我在pickerviewcontroller中获得了上面的代码,并在loadView方法中设置了pickerview.delegate=self。我尝试了didSelectRow方法:UIView*selectedView=[numberFactsPicker viewForRow:numberRow forComponent:kFactComponent];selectedView.backgroundColor=[UIColor redColor];不知何故,我想我可能需要将其添加回子视图?也许您可以检查*selectedView是否为零。也许有些东西没有连接好。也许可以使用调试器或NSLog语句来检查事物的值,以及确保正在调用委托方法。设置viewForRow的背景色通常有效,但对于第一个和最后一个项目,您仍然可以看到捡拾器的白色背景,远离卷轴的边缘。遗憾的是,设置[myPickerView setBackGroundColor:[UIColor blackColor]]或任何颜色都不能满足人们的期望。在UIPickerView的viewForRow方法中,这对我来说是可行的。文本标签有颜色,行背景也有颜色。唯一需要注意的是,在创建选择器视图时,需要强制选择,以便初始显示也具有带有颜色的选定行。