简体   繁体   English

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

[英]Changing Background color of Picker View

How can i change the Color of my Picker view depending on the background color of the TextField? 如何根据TextField的背景颜色更改Picker视图的颜色? eg i have 2 text fields, The one is red and the other one is blue, If i clicked the red one the picker background is red also, And if i click the blue one the picker background will be blue also? 例如,我有2个文本字段,一个是红色,另一个是蓝色,如果我单击红色,则选择器背景也是红色,如果我单击蓝色,则选择器背景也会是蓝色? im using this code for the text field to show the picker view. 我在文本字段中使用此代码来显示选择器视图。

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

thank you! 谢谢!

use self.picker.backgroundColor = [UIColor redColor]; 使用self.picker.backgroundColor = [UIColor redColor]; in textFieldDidBeginEditing: UITextFieldDelegate method and change color using by using tag property. textFieldDidBeginEditing: UITextFieldDelegate方法,并通过使用标记属性来更改颜色。

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

 //use default value

} }

Edit: 编辑:

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

add different background colour for each case 为每种情况添加不同的背景色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改键盘替换的Picker视图的背景颜色 - Changing background color of keyboard-replaced Picker View 更改父视图控制器的背景色 - Changing the Parent View Controller's Background Color 根据 SwiftUI 中的另一个视图更改背景颜色? - Changing background color based on another View in SwiftUI? 移动野生动物园:更改选择器视图的背景颜色(选择) - mobile safari: change background color of picker view (select) iPhone - 改变视图后面的UIWebView背景颜色(弹跳背景) - iPhone - changing UIWebView background color behind the view (bouncing background) 集合视图选定的单元格背景视图颜色在 IOS 15 中未更改 - Collection view selected cell background view color not changing in IOS 15 从一个视图控制器更改项目中所有视图的背景颜色? - Changing background color of all views in project from one view controller? 当视图控制器完成加载时,UIView背景颜色发生变化 - UIView background color changing when view controller finishes loading 在表格视图iPhone的首次加载时更改选定行的背景颜色 - Changing background color for selected row on first load of table view iphone Xcode颜色选择器自动更改HSB颜色 - Xcode Color picker automatically changing HSB color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM