Jack Solomon
Jack Solomon

Reputation: 890

iOS 8 Text Field Tint Color

In iOS 7, changing the 'tint color' attribute of a uitextfield would change the cursor color of that text field. In iOS 8, even when I change the global storyboard tint color, this does not occur (objective-c, still works in iOS 7). How do I fix this?

Upvotes: 8

Views: 5816

Answers (3)

Albert Renshaw
Albert Renshaw

Reputation: 17902

Looking to actually put a tinted color filter on the whole view, not just change cursor color?

Look no further. .tint is a lame name because it in no way implies that it adjusts the cursor color. Naturally, people googling about the .tint property are likely trying to find a way to apply a color filter across the whole frame/region of their UIView, UITextView, whatever.

Here is my solution for you:

I made macros for this purpose:

#define removeTint(view) \
if ([((NSNumber *)[view.layer valueForKey:@"__hasTint"]) boolValue]) {\
for (CALayer *layer in [view.layer sublayers]) {\
if ([((NSNumber *)[layer valueForKey:@"__isTintLayer"]) boolValue]) {\
[layer removeFromSuperlayer];\
break;\
}\
}\
}

#define setTint(view, tintColor) \
{\
if ([((NSNumber *)[view.layer valueForKey:@"__hasTint"]) boolValue]) {\
removeTint(view);\
}\
[view.layer setValue:@(YES) forKey:@"__hasTint"];\
CALayer *tintLayer = [CALayer new];\
tintLayer.frame = view.bounds;\
tintLayer.backgroundColor = [tintColor CGColor];\
[tintLayer setValue:@(YES) forKey:@"__isTintLayer"];\
[view.layer addSublayer:tintLayer];\
}

To use, simply just call:

setTint(yourView, yourUIColor);
//Note: include opacity of tint in your UIColor using the alpha channel (RGBA), e.g. [UIColor colorWithRed:0.5f green:0.0 blue:0.0 alpha:0.25f];

When removing the tint simply call:

removeTint(yourView);

Upvotes: 0

George
George

Reputation: 221

try the following:

[[self.textField setTintColor:[UIColor blueColor]];

[self.textField setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];

Upvotes: 8

Niccolò Passolunghi
Niccolò Passolunghi

Reputation: 6024

I just tried to replicate your problem but both on iOS7.1 and iOS8 the tintColor attribute of a textfield works perfectly.

This line of code change the cursor color of the textField. Try this instead of changing the tint color in Storyboard

textField.tintColor = [UIColor colorWithRed:98.0/255.0f green:98.0/255.0f blue:98.0/255.0f alpha:1.0];

Hope it helps!

Upvotes: 12

Related Questions