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












18















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! :)










share|improve this question























  • What color was the cancel button before?

    – cbay
    Feb 9 '16 at 21:46











  • @cbay It was the default colour which is like a light/faint gray

    – Nick89
    Feb 9 '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 9 '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 9 '16 at 21:54











  • stackoverflow.com/questions/19651374/… stackoverflow.com/questions/27838084/… might help you.

    – cbay
    Feb 9 '16 at 22:04


















18















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! :)










share|improve this question























  • What color was the cancel button before?

    – cbay
    Feb 9 '16 at 21:46











  • @cbay It was the default colour which is like a light/faint gray

    – Nick89
    Feb 9 '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 9 '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 9 '16 at 21:54











  • stackoverflow.com/questions/19651374/… stackoverflow.com/questions/27838084/… might help you.

    – cbay
    Feb 9 '16 at 22:04
















18












18








18


2






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! :)










share|improve this question














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! :)







ios swift uisearchbar






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 9 '16 at 21:42









Nick89Nick89

1,32051946




1,32051946













  • What color was the cancel button before?

    – cbay
    Feb 9 '16 at 21:46











  • @cbay It was the default colour which is like a light/faint gray

    – Nick89
    Feb 9 '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 9 '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 9 '16 at 21:54











  • stackoverflow.com/questions/19651374/… stackoverflow.com/questions/27838084/… might help you.

    – cbay
    Feb 9 '16 at 22:04





















  • What color was the cancel button before?

    – cbay
    Feb 9 '16 at 21:46











  • @cbay It was the default colour which is like a light/faint gray

    – Nick89
    Feb 9 '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 9 '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 9 '16 at 21:54











  • stackoverflow.com/questions/19651374/… stackoverflow.com/questions/27838084/… might help you.

    – cbay
    Feb 9 '16 at 22:04



















What color was the cancel button before?

– cbay
Feb 9 '16 at 21:46





What color was the cancel button before?

– cbay
Feb 9 '16 at 21:46













@cbay It was the default colour which is like a light/faint gray

– Nick89
Feb 9 '16 at 21:48





@cbay It was the default colour which is like a light/faint gray

– Nick89
Feb 9 '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 9 '16 at 21:51





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 9 '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 9 '16 at 21:54





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 9 '16 at 21:54













stackoverflow.com/questions/19651374/… stackoverflow.com/questions/27838084/… might help you.

– cbay
Feb 9 '16 at 22:04







stackoverflow.com/questions/19651374/… stackoverflow.com/questions/27838084/… might help you.

– cbay
Feb 9 '16 at 22:04














10 Answers
10






active

oldest

votes


















23














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)





share|improve this answer





















  • 1





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

    – LightNight
    Oct 27 '16 at 15:14











  • Does this affect anything else in the app or just the cancel button?

    – Arthur Garza
    Nov 3 '16 at 0:37






  • 1





    @ArthurGarza it only effects the cancel button

    – Nick89
    Nov 3 '16 at 14:12











  • @Nick89 Thanks guy! I'm very happy!!!!! Work fine for me!

    – Victor Gomes
    Nov 18 '16 at 19:30





















11














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






share|improve this answer
























  • Flawless victory. Xcode 9.4.1, Swift 4.0, iOS 11.4.1

    – eGanges
    Jul 24 '18 at 15:36











  • Just to simplify a bit: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal)

    – Rufat Mirza
    Aug 29 '18 at 8:55





















10














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])






share|improve this answer
























  • This should be the accepted answer

    – Nikhil Sridhar
    Apr 11 '18 at 23:43











  • 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





















7














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






share|improve this answer
























  • I have yes, It doesn't change the colour of the text. It just adds a faint outline to the button area.

    – Nick89
    Feb 9 '16 at 22:00











  • you got a sample app you can upload?

    – David Yang Liu
    Feb 9 '16 at 22:12











  • @DavidYangLiu it also changes the cursor color?

    – Vishal16
    Feb 19 '18 at 7:04





















4














Swift 4.2



UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)





share|improve this answer































    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)





    share|improve this answer































      3














      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)





      share|improve this answer

































        1














        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






        share|improve this answer































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






          share|improve this answer

































            -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






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35302760%2fhow-to-change-the-colour-of-the-cancel-button-on-the-uisearchbar-in-swift%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              10 Answers
              10






              active

              oldest

              votes








              10 Answers
              10






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              23














              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)





              share|improve this answer





















              • 1





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

                – LightNight
                Oct 27 '16 at 15:14











              • Does this affect anything else in the app or just the cancel button?

                – Arthur Garza
                Nov 3 '16 at 0:37






              • 1





                @ArthurGarza it only effects the cancel button

                – Nick89
                Nov 3 '16 at 14:12











              • @Nick89 Thanks guy! I'm very happy!!!!! Work fine for me!

                – Victor Gomes
                Nov 18 '16 at 19:30


















              23














              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)





              share|improve this answer





















              • 1





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

                – LightNight
                Oct 27 '16 at 15:14











              • Does this affect anything else in the app or just the cancel button?

                – Arthur Garza
                Nov 3 '16 at 0:37






              • 1





                @ArthurGarza it only effects the cancel button

                – Nick89
                Nov 3 '16 at 14:12











              • @Nick89 Thanks guy! I'm very happy!!!!! Work fine for me!

                – Victor Gomes
                Nov 18 '16 at 19:30
















              23












              23








              23







              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)





              share|improve this answer















              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)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 12 '18 at 9:33









              Jack

              6,30833659




              6,30833659










              answered Feb 10 '16 at 20:58









              Nick89Nick89

              1,32051946




              1,32051946








              • 1





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

                – LightNight
                Oct 27 '16 at 15:14











              • Does this affect anything else in the app or just the cancel button?

                – Arthur Garza
                Nov 3 '16 at 0:37






              • 1





                @ArthurGarza it only effects the cancel button

                – Nick89
                Nov 3 '16 at 14:12











              • @Nick89 Thanks guy! I'm very happy!!!!! Work fine for me!

                – Victor Gomes
                Nov 18 '16 at 19:30
















              • 1





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

                – LightNight
                Oct 27 '16 at 15:14











              • Does this affect anything else in the app or just the cancel button?

                – Arthur Garza
                Nov 3 '16 at 0:37






              • 1





                @ArthurGarza it only effects the cancel button

                – Nick89
                Nov 3 '16 at 14:12











              • @Nick89 Thanks guy! I'm very happy!!!!! Work fine for me!

                – Victor Gomes
                Nov 18 '16 at 19:30










              1




              1





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

              – LightNight
              Oct 27 '16 at 15:14





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

              – LightNight
              Oct 27 '16 at 15:14













              Does this affect anything else in the app or just the cancel button?

              – Arthur Garza
              Nov 3 '16 at 0:37





              Does this affect anything else in the app or just the cancel button?

              – Arthur Garza
              Nov 3 '16 at 0:37




              1




              1





              @ArthurGarza it only effects the cancel button

              – Nick89
              Nov 3 '16 at 14:12





              @ArthurGarza it only effects the cancel button

              – Nick89
              Nov 3 '16 at 14:12













              @Nick89 Thanks guy! I'm very happy!!!!! Work fine for me!

              – Victor Gomes
              Nov 18 '16 at 19:30







              @Nick89 Thanks guy! I'm very happy!!!!! Work fine for me!

              – Victor Gomes
              Nov 18 '16 at 19:30















              11














              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






              share|improve this answer
























              • Flawless victory. Xcode 9.4.1, Swift 4.0, iOS 11.4.1

                – eGanges
                Jul 24 '18 at 15:36











              • Just to simplify a bit: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal)

                – Rufat Mirza
                Aug 29 '18 at 8:55


















              11














              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






              share|improve this answer
























              • Flawless victory. Xcode 9.4.1, Swift 4.0, iOS 11.4.1

                – eGanges
                Jul 24 '18 at 15:36











              • Just to simplify a bit: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal)

                – Rufat Mirza
                Aug 29 '18 at 8:55
















              11












              11








              11







              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






              share|improve this answer













              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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 19 '18 at 11:55









              Incredible_DevIncredible_Dev

              754411




              754411













              • Flawless victory. Xcode 9.4.1, Swift 4.0, iOS 11.4.1

                – eGanges
                Jul 24 '18 at 15:36











              • Just to simplify a bit: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal)

                – Rufat Mirza
                Aug 29 '18 at 8:55





















              • Flawless victory. Xcode 9.4.1, Swift 4.0, iOS 11.4.1

                – eGanges
                Jul 24 '18 at 15:36











              • Just to simplify a bit: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal)

                – Rufat Mirza
                Aug 29 '18 at 8:55



















              Flawless victory. Xcode 9.4.1, Swift 4.0, iOS 11.4.1

              – eGanges
              Jul 24 '18 at 15:36





              Flawless victory. Xcode 9.4.1, Swift 4.0, iOS 11.4.1

              – eGanges
              Jul 24 '18 at 15:36













              Just to simplify a bit: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal)

              – Rufat Mirza
              Aug 29 '18 at 8:55







              Just to simplify a bit: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal)

              – Rufat Mirza
              Aug 29 '18 at 8:55













              10














              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])






              share|improve this answer
























              • This should be the accepted answer

                – Nikhil Sridhar
                Apr 11 '18 at 23:43











              • 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


















              10














              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])






              share|improve this answer
























              • This should be the accepted answer

                – Nikhil Sridhar
                Apr 11 '18 at 23:43











              • 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
















              10












              10








              10







              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])






              share|improve this answer













              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])







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 26 '17 at 4:43









              saturngodsaturngod

              16.4k145385




              16.4k145385













              • This should be the accepted answer

                – Nikhil Sridhar
                Apr 11 '18 at 23:43











              • 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





















              • This should be the accepted answer

                – Nikhil Sridhar
                Apr 11 '18 at 23:43











              • 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



















              This should be the accepted answer

              – Nikhil Sridhar
              Apr 11 '18 at 23:43





              This should be the accepted answer

              – Nikhil Sridhar
              Apr 11 '18 at 23:43













              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







              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













              7














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






              share|improve this answer
























              • I have yes, It doesn't change the colour of the text. It just adds a faint outline to the button area.

                – Nick89
                Feb 9 '16 at 22:00











              • you got a sample app you can upload?

                – David Yang Liu
                Feb 9 '16 at 22:12











              • @DavidYangLiu it also changes the cursor color?

                – Vishal16
                Feb 19 '18 at 7:04


















              7














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






              share|improve this answer
























              • I have yes, It doesn't change the colour of the text. It just adds a faint outline to the button area.

                – Nick89
                Feb 9 '16 at 22:00











              • you got a sample app you can upload?

                – David Yang Liu
                Feb 9 '16 at 22:12











              • @DavidYangLiu it also changes the cursor color?

                – Vishal16
                Feb 19 '18 at 7:04
















              7












              7








              7







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






              share|improve this answer













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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 9 '16 at 21:55









              David Yang LiuDavid Yang Liu

              8471719




              8471719













              • I have yes, It doesn't change the colour of the text. It just adds a faint outline to the button area.

                – Nick89
                Feb 9 '16 at 22:00











              • you got a sample app you can upload?

                – David Yang Liu
                Feb 9 '16 at 22:12











              • @DavidYangLiu it also changes the cursor color?

                – Vishal16
                Feb 19 '18 at 7:04





















              • I have yes, It doesn't change the colour of the text. It just adds a faint outline to the button area.

                – Nick89
                Feb 9 '16 at 22:00











              • you got a sample app you can upload?

                – David Yang Liu
                Feb 9 '16 at 22:12











              • @DavidYangLiu it also changes the cursor color?

                – Vishal16
                Feb 19 '18 at 7:04



















              I have yes, It doesn't change the colour of the text. It just adds a faint outline to the button area.

              – Nick89
              Feb 9 '16 at 22:00





              I have yes, It doesn't change the colour of the text. It just adds a faint outline to the button area.

              – Nick89
              Feb 9 '16 at 22:00













              you got a sample app you can upload?

              – David Yang Liu
              Feb 9 '16 at 22:12





              you got a sample app you can upload?

              – David Yang Liu
              Feb 9 '16 at 22:12













              @DavidYangLiu it also changes the cursor color?

              – Vishal16
              Feb 19 '18 at 7:04







              @DavidYangLiu it also changes the cursor color?

              – Vishal16
              Feb 19 '18 at 7:04













              4














              Swift 4.2



              UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)





              share|improve this answer




























                4














                Swift 4.2



                UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)





                share|improve this answer


























                  4












                  4








                  4







                  Swift 4.2



                  UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)





                  share|improve this answer













                  Swift 4.2



                  UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 5 '18 at 8:30









                  Abhishek JainAbhishek Jain

                  2,54221226




                  2,54221226























                      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)





                      share|improve this answer




























                        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)





                        share|improve this answer


























                          3












                          3








                          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)





                          share|improve this answer













                          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)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 25 '18 at 14:45









                          TinoTino

                          9924




                          9924























                              3














                              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)





                              share|improve this answer






























                                3














                                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)





                                share|improve this answer




























                                  3












                                  3








                                  3







                                  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)





                                  share|improve this answer















                                  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)






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Jan 1 at 17:24









                                  Krishna Raj Salim

                                  5,60252455




                                  5,60252455










                                  answered May 31 '18 at 15:26









                                  MattMatt

                                  356521




                                  356521























                                      1














                                      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






                                      share|improve this answer




























                                        1














                                        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






                                        share|improve this answer


























                                          1












                                          1








                                          1







                                          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






                                          share|improve this answer













                                          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







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Apr 16 '18 at 7:12









                                          RamRam

                                          299315




                                          299315























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






                                              share|improve this answer






























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






                                                share|improve this answer




























                                                  0












                                                  0








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






                                                  share|improve this answer















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







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Feb 26 at 10:09

























                                                  answered Feb 26 at 10:04









                                                  Lok SNLok SN

                                                  788




                                                  788























                                                      -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






                                                      share|improve this answer




























                                                        -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






                                                        share|improve this answer


























                                                          -1












                                                          -1








                                                          -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






                                                          share|improve this answer













                                                          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







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jul 16 '18 at 6:45









                                                          KamranKamran

                                                          6,90521029




                                                          6,90521029






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35302760%2fhow-to-change-the-colour-of-the-cancel-button-on-the-uisearchbar-in-swift%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              MongoDB - Not Authorized To Execute Command

                                                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                                              How to fix TextFormField cause rebuild widget in Flutter