我想知道在iOS中改变UITextField(和UITextView如果答案相同)中的光标/插入符号的颜色。我看到过OSX开发的答案,但没有iOS的答案。

这可能吗?


当前回答

一个更通用的方法是设置UIView的外观的tintColor。

UIColor *myColor = [UIColor purpleColor];
[[UIView appearance] setTintColor:myColor];

如果您使用许多默认UI元素,这是有意义的。

其他回答

这在swift中很管用:

UITextField.tintColor = UIColor.blackColor()

你也可以在storyboard中设置:https://stackoverflow.com/a/18759577/3075340

如果你的目标系统是iOS 7+,这就简单多了。简单地改变tintColor字段的游标使用外观代理,它将应用于整个应用程序:

斯威夫特3.0:

UITextField.appearance().tintColor = .black 

objective - c:

[[UITextField appearance] setTintColor:[UIColor blackColor]];

同样的答案适用于一个单独的UITextField:

斯威夫特3.0:

myTextField.tintColor = .black 

objective - c

[myTextField setTintColor:[UIColor blackColor]];

一个更通用的方法是设置UIView的外观的tintColor。

UIColor *myColor = [UIColor purpleColor];
[[UIView appearance] setTintColor:myColor];

如果您使用许多默认UI元素,这是有意义的。

杜格什的方法确实有效。

我也多次使用这样的KVC解决方案。 尽管它似乎没有证件,但它是有效的。坦白地说,你什么都不用 private方法-只有键值编码,这是合法的。

附注:昨天我的新应用出现在AppStore时,这种方法没有任何问题。当我使用KVC改变一些只读属性(如navigatonBar)或私有ivars时,这并不是第一次。

如果UITextField来自UISearchBar,那么首先从searchBar获得textField,然后应用tintColor属性:

let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.tintColor = UIColor.lightGray