在iOS 7中,更改uitextfield的“tint color”属性将更改该文本字段的光标颜色。在iOS 8中,即使我更改全局故事板的色调颜色,也不会发生这种情况(使用objective-c,在iOS 7中仍然有效)。我该如何解决这个问题?
我刚刚尝试复制了你的问题,但在iOS7.1和iOS8上,文本字段的tintColor属性都可以完美地工作。
这行代码可以更改textField的光标颜色。与在Storyboard中更改色调颜色相比,请尝试使用此方法。
textField.tintColor = [UIColor colorWithRed:98.0/255.0f green:98.0/255.0f blue:98.0/255.0f alpha:1.0];
希望能对你有所帮助!
[[self.textField setTintColor:[UIColor blueColor]];
[self.textField setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
self.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
,tintColor 将不起作用。 - Kamil Powałowski请看这里。 .tint
是一个糟糕的名字,因为它无法暗示它会调整光标的颜色。自然地,搜索 .tint
属性的人们可能正在尝试找到一种方法来在他们的UIView、UITextView或其他任何区域中应用颜色过滤器。
这是我的解决方案:
我为此目的制作了宏:
#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];\
}
使用时,只需调用:
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];
当移除色调时,只需调用:
removeTint(yourView);