ios


UISearchController change 'Cancel' button title


How we can change the title of cancel button in search controller?
You can change the "Cancel" Button in search bar using this-
for (UIView *view in searchBar.subviews)
{
for (id subview in view.subviews)
{
if ( [subview isKindOfClass:[UIButton class]] )
{
[subview setEnabled:YES];
UIButton *cancelButton = (UIButton*)subview;
[cancelButton setTitle:#"hi" forState:UIControlStateNormal];
NSLog(#"enableCancelButton");
return;
}
}
}
The solution provided above could change with new iOS releases. Here is a better approach:
[searchBar setValue:#"customString" forKey:#"_cancelButtonText"];
You should use the appearance proxy of the UISearchBar. You can find an example here - How to change the default text of Cancel Button which appears in the UISearchBar +iPhone
The Swift equivalent of Burhanuddin Sunelwala answer did the trick for me!
self.searchBar.setValue("custom string", forKey: "cancelButtonText")
Thanks to Burhanuddin Sunelwala for putting me in the right direction!
for (UIView *subView in SearchBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
[cancelButton setTitle:#"TitleString" forState:UIControlStateNormal];
}
swift version:
for view:UIView in (searchView?.subviews)!
{
for subView:UIView in (view.subviews)
{
if ( subView is UIButton )
{
let cancelBut = subView as! UIButton
//do stuff with cancelButton here
}
}
}
Worth noting, that the preferred method for changing the Cancel button title is now via appearances (got the idea from an another question: http://stackoverflow.com/a/34522163/511878):
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setTitle:#"Annuler"];
You also need to have the searchBar setShowsCancelButton before the procedure.
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
[theSearchBar setShowsCancelButton:YES animated:NO];
for (UIView *subView in theSearchBar.subviews){
if([subView isKindOfClass:[UIButton class]]){
[(UIButton*)subView setTitle:#"Done" forState:UIControlStateNormal];
}
}
}
Note also use UIButton to avoid problems with Apple!

Related Links

iOS different alpha effect between CGPathMoveToPoint and CGPathAddArc
Unwind of custom segue not triggered
SceneKit Physics Contact
How do a control alert for a TextField in Swift 3
How to retrieve Email from Facebook SDK
UINavigationController Within UIPageViewController Issue
How do I run Backendless with Swift 3?
Set Eureka DateTimeRow with specific time
NSJSONSerialization: “No value” error
Can I have multiple leftOverlay items in UITextField or 2 independent texts at the same time?
Is it possible to parse a X509 certificate with Alamofire?
urlNode.objectForKeyedSubscript(“href”) as? String - Swift 2.3 / Swift 3 error
UITextView will shorten text?
App Store Shows English UK Screenshot Instead of my English US Screen Shot
Running appium on iOS device with tests on a Windows machine
Why is PHImageRequestOptions.progressHandler never called?

Categories

HOME
sharepoint-2013
pyqt5
openshift-origin
youtube
chronicle
stripe-payments
phpmailer
freebsd
microsoft-dynamics
range
superfish
cobol
pouchdb
command-line-interface
command-line-arguments
signature
zend-expressive
facebook-opengraph
group-by
cloudrail
python-2.5
silk-performer
git-bash
advantage-database-server
jmeter-plugins
revitpythonshell
opencms
wso2cep
intersystems
yii1.x
esri
angular-ngmodel
launchpad
popupwindow
gcp
aem-6
google-domain-api
gradient-descent
microsoft-translator
sourcetree
virtualmin
invalidate
multisite
xssf
orchestrate
jasmine2.0
kony
attributerouting
reactivex
nuget-package-restore
email-notifications
masspay
lapply
django-errors
ultratree
vb4android
data-fitting
dashing
agents-jade
menustrip
vsvim
gcloud-java
yapdatabase
strace
win32com
laravel-forge
linq2db
nbug
spatial-index
stringr
omap
ehcache-bigmemory
textmatching
alt
diagonal
android-tablelayout
iscroll4
httpservice
bessel-functions
linguistics
bunny
image-scaling
modalpopupextender
xml.modify
qt4.7
inputview
showcaseview
mod-python
prudentia
xcache
hla
xhtml-transitional
neventstore
tempo
memento
hudson-plugins
squirrelmail
mixing
calloc
hiphop
c++builder-xe2
program-files
jtemplates
sharepoint-feature
greensoftware
corporate-policy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App