How to change the colour of the 'Cancel' button on the UISearchBar in Swift
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:
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
|
show 1 more comment
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:
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
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
|
show 1 more comment
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:
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
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:
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
ios swift uisearchbar
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
|
show 1 more comment
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
|
show 1 more comment
10 Answers
10
active
oldest
votes
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)
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
add a comment |
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
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
add a comment |
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])
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
add a comment |
did you try
UISearchBar.appearance().tintColor = UIColor.whiteColor()
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
add a comment |
Swift 4.2
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
add a comment |
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)
add a comment |
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)
add a comment |
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
for SearchBar cancel button colour
add a comment |
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:'
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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])
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
add a comment |
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])
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
add a comment |
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])
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])
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
add a comment |
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
add a comment |
did you try
UISearchBar.appearance().tintColor = UIColor.whiteColor()
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
add a comment |
did you try
UISearchBar.appearance().tintColor = UIColor.whiteColor()
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
add a comment |
did you try
UISearchBar.appearance().tintColor = UIColor.whiteColor()
did you try
UISearchBar.appearance().tintColor = UIColor.whiteColor()
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
add a comment |
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
add a comment |
Swift 4.2
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
add a comment |
Swift 4.2
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
add a comment |
Swift 4.2
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
Swift 4.2
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
answered Nov 5 '18 at 8:30
Abhishek JainAbhishek Jain
2,54221226
2,54221226
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Jun 25 '18 at 14:45


TinoTino
9924
9924
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Jan 1 at 17:24


Krishna Raj Salim
5,60252455
5,60252455
answered May 31 '18 at 15:26
MattMatt
356521
356521
add a comment |
add a comment |
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
for SearchBar cancel button colour
add a comment |
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
for SearchBar cancel button colour
add a comment |
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
for SearchBar cancel button colour
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
for SearchBar cancel button colour
answered Apr 16 '18 at 7:12
RamRam
299315
299315
add a comment |
add a comment |
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:'
add a comment |
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:'
add a comment |
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:'
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:'
edited Feb 26 at 10:09
answered Feb 26 at 10:04
Lok SNLok SN
788
788
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jul 16 '18 at 6:45


KamranKamran
6,90521029
6,90521029
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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