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

这可能吗?


当前回答

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

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

其他回答

斯威夫特4

在viewDidLoad()中只调用下面的代码:

代码示例

//txtVComplaint is a textView

txtVComplaint.tintColor = UIColor.white

txtVComplaint.tintColorDidChange()

试试,这对我很有用。

[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] strong textforKey:@"insertionPointColor"];

这在swift中很管用:

UITextField.tintColor = UIColor.blackColor()

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

杜格什的方法确实有效。

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

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

在iOS7中,你可以简单地改变textField的tintColor