How to Change the Color of Uipickerview Selector

Change colour of UIPickerView selection indicators

The best solution I can offer is to place 2 UIImages on top of your picker wheel at the width of 1 and length set to your preference. This is how I overcame this.

It does not work well if you require multiple size views so programatically doing it to cater for your size changes would be the next best solution. To programatically change the size using if statements:

How to resize the image programmatically in objective-c in iphone

If you change the size based on orientation add an if statement that handles orientation like so:

 if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}

Kind regards

How to change the color of UIPickerView Selector

I suppose you're dealing with the iPhone SDK? There may be some other frameworks which uses this name, so maybe you can add your tags to include uikit, cocoa-touch or something.

Anyway, you can set showsSelectionIndicator of the UIPickerView instance to NO, so it hides the selector. Then you can create a new view with the adjusted selection style, and add it to the superview above the UIPickerView.

// Some sample code, but you can do this in IB if you want to
_pickerView = [[UIPickerView alloc] init];
_pickerView.showsSelectionIndicator = NO;
[_pickerView sizeToFit];
[self.view addSubview:_pickerView];

UIImage *selectorImage = [UIImage imageNamed:@"selectorImage.png"]; // You have to make it strechable, probably
UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
customSelector.frame = CGRectZero; // Whatever rect to match the UIImagePicker
[self.view addSubview:customSelector];
[customSelector release];

Hacking the UI Element itself will take much more work, and this has to work as well.

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:
enter image description here

How to change color of selected row in UIPickerView

Solved! Declare 2 instance variables: selectedView, and oldView. Then the following code does the trick:

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;

Change colour of UIPickerView selection indicators

The best solution I can offer is to place 2 UIImages on top of your picker wheel at the width of 1 and length set to your preference. This is how I overcame this.

It does not work well if you require multiple size views so programatically doing it to cater for your size changes would be the next best solution. To programatically change the size using if statements:

How to resize the image programmatically in objective-c in iphone

If you change the size based on orientation add an if statement that handles orientation like so:

 if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}

Kind regards



Related Topics



Leave a reply



Submit