xcode iOS 8 文本字段色调颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26158171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:46:00  来源:igfitidea点击:

iOS 8 Text Field Tint Color

iosobjective-cxcodeios8tint

提问by Hyman Solomon

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?

在 iOS 7 中,更改 uitextfield 的 'tint color' 属性将更改该文本字段的光标颜色。在 iOS 8 中,即使我更改了全局故事板色调颜色,也不会发生这种情况(objective-c,在 iOS 7 中仍然有效)。我该如何解决?

回答by Niccolò Passolunghi

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

我只是试图复制您的问题,但在 iOS7.1 和 iOS8 上,文本字段的 tintColor 属性都可以完美运行。

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

这行代码改变了 textField 的光标颜色。试试这个,而不是改变 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!

希望能帮助到你!

回答by George

try the following:

尝试以下操作:

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

[self.textField setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];

回答by Albert Renshaw

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

想要在整个视图上实际放置一个有色滤色器,而不仅仅是更改光标颜色?

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

别再看了。.tint是一个蹩脚的名字,因为它绝不意味着它会调整光标颜色。自然地,在谷歌上搜索该.tint属性的人可能会试图找到一种方法来在其 UIView、UITextView 等的整个框架/区域中应用滤色器。

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