Strange Custom Background Color on Uipickerview Swift

Strange Custom Background Color on UIPickerView Swift

tkanzakic's answer finally allowed me to change the background color of UIDatePicker.

Here's the Swift 4 version:

class CustomDatePicker: UIDatePicker {

var customBackgroundColor = UIColor.black

override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)

if newWindow != nil {
inputView?.backgroundColor = customBackgroundColor
}
}
}

Change the customBackgroundColor property to your liking.

Custom UIPickerView with Custom Background color

Addition to Nina's answer, below are some of the good custom Picker view controls which will be good to use in terms of performance and customizable.

  1. http://www.cocoacontrols.com/platforms/ios/controls/cppickerview
  2. http://www.cocoacontrols.com/platforms/ios/controls/afpickerview
  3. http://www.cocoacontrols.com/platforms/ios/controls/v8horizontalpickerview (Horizontal PickerView)

How to set the background color of UIPickerView on iOS 7 using SDK 7?

I have added UIView under UIPickerView with code:

CGRect framePickerView = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 216);
pickerView = [[[UIView alloc] initWithFrame:framePickerView] autorelease];
pickerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:pickerView];
[pickerView addSubview:picker];

instead the code:

[self.view addSubview:picker];

How to set background color of UIPickerView using UIAppearance?

As per my Knowledge UIPicker does not conforms to UI_APPEARANCE_SELECTOR for UIAppereance ..

[pickerView setBackgroundColor:[UIColor blueColor]];

OR You Might want use..

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

Above two are the solutions i would prefer to change Picker Appearance..There although an other way to set an image, but i won't suggest to use that.

Its up to you NOW..

Changing Background color of Picker View

use self.picker.backgroundColor = [UIColor redColor]; in textFieldDidBeginEditing: UITextFieldDelegate method and change color using by using tag property.

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

How to change UIPickerView title text color based on position

Yes, it is. Below example is for one component.

var pickerArray = [String]()
var selectedRow: Int {
return pickerView.selectedRow(inComponent: 0)
}

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

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
var color = UIColor.gray

if row == selectedRow {
color = UIColor.blue
} else if selectedRow == row - 1 || selectedRow == row + 1 {
color = UIColor.black
}

return NSAttributedString(string: pickerArray[row], attributes: [NSAttributedString.Key.foregroundColor: color])
}

Maybe it exists another way, but I think this will be sufficient :]

// Update1: NOT RECOMMENDED

You could override (hitTest:point:event) and return self from it and then reload the components on (touchesMoved:touches:event). BUT the scroll and touches on the picker will not work. You will need to do something else :(

// Update2: The result:
Sample Image



Related Topics



Leave a reply



Submit