更改选择器视图的背景颜色

时间:2013-11-22 09:22:10

标签: ios uipickerview

如何根据TextField的背景颜色更改拾取器视图的颜色?例如我有2个文本字段,一个是红色,另一个是蓝色,如果我点击红色,选择器背景也是红色,如果我点击蓝色,选择器背景也将是蓝色?我在文本字段中使用此代码来显示选择器视图。

    int tag = [(UITextField*)sender tag];
self.myPicker.hidden = NO;
selectedTable = tag;
[sender resignFirstResponder];
float yy = 10;
switch (tag) {
    case 0: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
        break;
    case 1: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
        break;
    case 2: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
        break;
    case 3: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 4: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 5: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 6: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 7: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 8: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
        break;


    default:
        break;
}
谢谢你!

1 个答案:

答案 0 :(得分:0)

self.picker.backgroundColor = [UIColor redColor]; textFieldDidBeginEditing:方法中使用UITextFieldDelegate,并使用tag属性更改颜色。

if(textField.tag == 1){
     self.picker.backgroundColor = [UIColor redColor];
}
else if(textField.tag == 2){
  self.picker.backgroundColor = [UIColor redColor];
}
else{

 //use default value

}

编辑:

switch (tag) {
    case 0: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
            self.picker.backgroundColor = [UIColor redColor];
        break;
    case 1: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
            self.picker.backgroundColor = [UIColor bluecolor];
        break;
    case 2: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
            self.picker.backgroundColor = [UIColor greenColor];
        break;
    case 3: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 4: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 5: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 6: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 7: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 8: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
        break;


    default:
        break;
}

为每个案例添加不同的背景颜色