在不更改tintColor的情况下更改UISearchBar上的光标颜色

时间:2014-07-29 04:34:29

标签: ios7 uisearchbar tintcolor

我希望我的searchBar的色调颜色为白色(意味着取消按钮为白色)。当着色颜色为白色时,光标不可见。有没有办法分别设置光标颜色?

12 个答案:

答案 0 :(得分:64)

将您的色调颜色设置为您想要取消按钮的颜色,然后使用UIAppearance Protocol将文本字段上的色调颜色更改为您希望光标所在的颜色。例如:

[self.searchBar setTintColor:[UIColor whiteColor]];                
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor darkGrayColor]];

答案 1 :(得分:13)

如果你喜欢斯威夫特的功能性但又烦人的单行,那么我就让本杰明回归到这个:

searchController.searchBar.tintColor = UIColor.whiteColor()

searchController.searchBar.subviews[0].subviews.flatMap(){ $0 as? UITextField }.first?.tintColor = UIColor.blueColor()

答案 2 :(得分:8)

Swift 3.0和4版本

searchController.searchBar.tintColor = .white        
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .black

请注意,searchBar不能是可选的。

答案 3 :(得分:6)

Compact Swift 2.0解决方案使用 for-where 语法(无需中断循环):

// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = UIColor.white()

// Loop into it's subviews and find TextField, change tint color to something else.
for subView in searchBar.subviews[0].subviews where subView.isKindOfClass(UITextField) {
        subView.tintColor = UIColor.darkTextColor()
}

答案 4 :(得分:5)

searchBar.tintColor = [UIColor whiteColor];
searchBar.backgroundColor = [UIColor clearColor];
for ( UIView *v in [searchBar.subviews.firstObject subviews] )
{
    if ( YES == [v isKindOfClass:[UITextField class]] )
    {
        [((UITextField*)v) setTintColor:[UIColor blueColor]];
        break;
    }
}

enter image description here

答案 5 :(得分:5)

这对我来说似乎也很快。

    searchController.searchBar.tintColor = UIColor.whiteColor()
    UITextField.appearanceWhenContainedInInstancesOfClasses([searchController.searchBar.dynamicType]).tintColor = UIColor.blackColor()

答案 6 :(得分:4)

对于那些希望在Swift中做同样事情的人来说,这是一个解决方案后我遇到了很多麻烦:

override func viewWillAppear(animated: Bool) {
    self.searchBar.tintColor = UIColor.whiteColor()

    let view: UIView = self.searchBar.subviews[0] as! UIView
    let subViewsArray = view.subviews

    for (subView: UIView) in subViewsArray as! [UIView] {
        println(subView)
        if subView.isKindOfClass(UITextField){
            subView.tintColor = UIColor.blueColor()
        }
    }

}

答案 7 :(得分:2)

我只需使用以下代码为UISearchBar添加扩展名。

extension UISearchBar {
    var cursorColor: UIColor! {
        set {
            for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
                subView.tintColor = newValue
            }
        }
        get {
            for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
                return subView.tintColor
            }
            // Return default tintColor
            return UIColor.eightBit(red: 1, green: 122, blue: 255, alpha: 100)
        }
    }
}

答案 8 :(得分:2)

iOS 9及以上版本

为取消按钮和textField设置tintColor的最简单方法,请使用:

self.searchBar setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:UIColor.blueColor];

答案 9 :(得分:1)

这是最简单的解决方案。

let textField = self.searchBar.value(forKey: "searchField") as! UITextField

    textField.tintColor = UIColor.white

答案 10 :(得分:0)

这是Felipe答案的“功能性”版本(Swift 4.2)

// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = .white

// Get the TextField subviews, change tint color to something else.
if let textFields = searchBar.subviews.first?.subviews.compactMap({ $0 as? UITextField }) {
        textFields.forEach { $0.tintColor = .darkGrey }
}

答案 11 :(得分:0)

在Swift 5中最简单:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black