30

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 in doing this, it seems to have also changed the colour of the 'Cancel' button to the right of it to the same colour. Ideally, I want the colour to be White.

This image shows how it currently looks:

It looks like there is no 'Cancel' button, but there is, its just the same colour as the searchBar (You can still press it etc)

Is there a way for me to change the colour of this 'Cancel button to white? Everything i've tried seems to make no difference.

Code i've used to make the UISearchBar this colour is:

UISearchBar.appearance().barTintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
UISearchBar.appearance().tintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)

And in the storyboard i've set these:

enter image description here

And finally, to make the placeholder, and text white inside the SearchBar, i've used:

for subView in self.filmSearchBar.subviews {

    for subsubView in subView.subviews {

        if let searchBarTextField = subsubView as? UITextField {

            searchBarTextField.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("Search Cinevu film reviews", comment: ""), attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])

            searchBarTextField.textColor = UIColor.whiteColor()

        }

    }

}

Thanks for any help! :)

Nick89
  • 2,948
  • 10
  • 31
  • 50
  • What color was the cancel button before? – cbay Feb 09 '16 at 21:46
  • @cbay It was the default colour which is like a light/faint gray – Nick89 Feb 09 '16 at 21:48
  • I haven't worked with search bars (and I do all my UI stuff programmatically), but where's the code for the cancel button? You should just be able to change the color with something like cancelButton.backgroundColor = UIColor.greyColor() – cbay Feb 09 '16 at 21:51
  • I was just looking at the docs. showsCancelButton defaults to NO. Try setting it to YES. I can't find where to change the color on it, if that's possible. – cbay Feb 09 '16 at 21:54
  • http://stackoverflow.com/questions/19651374/how-to-change-cancel-button-tint-color-of-uisearchbar-in-ios7 http://stackoverflow.com/questions/27838084/change-uibarbuttonitem-from-uisearchbar might help you. – cbay Feb 09 '16 at 22:04
  • I have written an answer to this topic here: http://stackoverflow.com/questions/19206757/how-to-change-textcolor-of-cancel-button-of-uisearchbar-in-ios7. Just use the SHSearchBar Cocoapod which is not such a pain in the ass like the UISearchBar. – blackjacx Nov 25 '16 at 12:39

12 Answers12

34

Having a look around, this seemed to be the best way to achieve what I needed:

 let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
 UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)
Jack
  • 13,571
  • 6
  • 76
  • 98
Nick89
  • 2,948
  • 10
  • 31
  • 50
17

Use this single line in code to change color of cancel button:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white], for: .normal)

Checked in Xcode 9.2 with Swift 4.0

Incredible_Dev
  • 956
  • 7
  • 13
11

Base on Nick89's code , I changed like that for Swift 3.1

let cancelButtonAttributes: [String: AnyObject] = [NSForegroundColorAttributeName: UIColor.white]

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

I want to change on UISearchBar only instead of all UIBarButton, so I'm using like UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])

saturngod
  • 24,649
  • 17
  • 62
  • 87
  • 1
    The same answer in **Swift 4**: `let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.gray] UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)` – Despotovic Oct 17 '18 at 14:29
8

did you try UISearchBar.appearance().tintColor = UIColor.whiteColor()

David Yang Liu
  • 1,170
  • 2
  • 10
  • 19
8

Swift 4.2

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
6

The Swift 4.2 version, based on other answers:

let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.orange]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
Matt
  • 674
  • 11
  • 25
5

I know this question does have enough answers but here is simple way to achieve, SearchBar Background colour and Cancel button background colour we can change directly it in storyboard Attributes inspector.

for SearchBar background colour

enter image description here

for SearchBar cancel button colour

enter image description here

Ram
  • 858
  • 1
  • 14
  • 19
3

With the Swift 4.0 RELEASE 2017-09-19 toolchain, this worked:

    let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
Tino
  • 117
  • 2
  • 4
2

You can simply set tintcolor of searchBar to change color of Cancel button. This is available since swift 4 so should do the trick.

let searchBar = UISearchBar(frame: )
searchBar.tintColor = .orange

Search bar cancel button and cursor color change

2

Swift 5

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([.foregroundColor : UIColor.barTint], for: .normal)
lexxnew
  • 21
  • 2
0

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:'

Lokesh SN
  • 1,583
  • 7
  • 23
-1

Swift 4.2, 4.1

A custom class added here can be used to customize the most common elements in a searchBar. Custom class SearchBar can result in to the following search bar.

enter image description here

Kamran
  • 14,987
  • 4
  • 33
  • 51