UISearchBar cancel button color?

前端 未结 11 636
耶瑟儿~
耶瑟儿~ 2020-12-13 09:00

When I drop a UISearchBar into my view inside Interface Builder, and change its style to Black Opaque, the cancel button stays unfittingly blue / gray and doesn\'t become bl

11条回答
  •  无人及你
    2020-12-13 09:30

    I have taken Benjamin's answer and combined it with safe Array lookup to produce a short, but safe functional version:

    searchController.searchBar.tintColor = UIColor.whiteColor()
    (searchController.searchBar.subviews[safe: 0]?.subviews as? [UIView])?
        .filter({$0.isKindOfClass(UITextField)})
        .map({$0.tintColor = .lightGrayColor()})
    

    This results in coloring the Cancel button white and the cursor when typing gray. Otherwise it would be white and thus not seen. The searchController is an object of type UISearchController. If anybody wants to use it inside the results controller, replace it with self.

    The implementation of the safe: subscript is nkukushkin's answer:

    extension Array {
        subscript(safe index: Int) -> T? {
            return indices(self) ~= index ? self[index] : nil
        }
    }
    

提交回复
热议问题