How to change the colour of the 'Cancel' button on the UISearchBar in Swift

后端 未结 12 1819
无人及你
无人及你 2020-12-29 20:28

I have added a UISearchBar to the top of my PFQueryTableViewController. I have changed the colour of the searchBar to be the colour of my app, but

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 21:25

    All the above answers didn't work for me. (Got 'Type 'NSAttributedString.Key' (aka 'NSString') has no member 'foregroundColor'' error)

    Maybe because I'm at Swift 3...

    Here's the slightly-modified code that worked for me:-

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : .black], for: .normal)
    

    Note:-

    If you are using UISearchController, insert this code to 'willPresentSearchController:' or 'didPresentSearchController:'

提交回复
热议问题