热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

更改ios8中的UISearchBar取消按钮文本-ChangeUISearchBarcancelbuttontextiniOS8

Idliketochangethetextfrom¨Cancel¨to¨Done¨oftheCancelbuttoninsidetheUISearchBariniO

I'd like to change the text from ¨Cancel¨to ¨Done¨ of the Cancel button inside the UISearchBar in iOS 8. I am using UISearchController. I've tried different approaches for iOS 6 and iOS 7 and they do not work. Has anybody done this?

我想把文本从¨¨¨¨完成的取消取消按钮在UISearchBar iOS中8。我用UISearchController。我为iOS 6和iOS 7尝试过不同的方法,但它们都不起作用。有人做过吗?

11 个解决方案

#1


68  

This will work

这将工作

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];

Swift Version:

斯威夫特版本:

searchBar.setValue("customString", forKey:"_cancelButtonText")

#2


14  

Found it!. in iOS 8, the cancel button is under the key "cancelButton". I used UISearchBar delegate searchBarTextDidBeginEditing to make the change.

发现它!在ios8中,取消按钮位于“cancelButton”键下。我使用UISearchBar委托searchBarTextDidBeginEditing进行更改。

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    let uiButton = searchBar.valueForKey("cancelButton") as UIButton
    uiButton.setTitle("Done", forState: UIControlState.Normal)

}

#3


12  

This worked for me in ios8, did not try in ios7, but should do the trick, beware of placing this line in the early times of the app cycle (eg : appDelegate)

这在ios8中对我有用,在ios7中没有尝试过,但是应该这样做,注意不要在应用周期的早期放置这条线(例如:appDelegate)

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Annuler"];

and as of ios9 you could also use

而ios9也可以用

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];

Swift version:

斯威夫特版本:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Annuler"

Hope that helps ;)

希望帮助。

#4


11  

Swift 3.0:

斯威夫特3.0:

In your AppDelegate add this:

在AppDelegate中添加如下内容:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "my text"

#5


9  

 for view in (UISearchBar.subviews[0]).subviews{
             if let button = view as? UIButton{
             button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
             button.setTitle("Cancel", forState:.Normal)
            }
     }

self.subviews[0]).subviews contain UISearchBarBackground,UISearchBarTextField,UINavigationButto‌​n in which UINavigationButton is subclass of UIButton.Check for View as UIButton and if yes then change property of UIButton.

self.subviews[0])。子视图包含UISearchBarBackground、UISearchBarTextField UINavigationButto‌UIButton的n UINavigationButton的子类。检查视图为UIButton,如果是,那么改变UIButton的属性。

#6


5  

In Swift:

迅速:

searchBar.setValue("customString", forKey: "_cancelButtonText")

Based on Burhanuddin Sunelwala answer.

基于Burhanuddin Sunelwala的回答。

#7


5  

I was having trouble getting this to work for a UISearchBar within a UISearchController. The text didn't change the first time the cancel button was shown but it did the second time.

我很难让它在UISearchBar中工作在UISearchController中。文本在第一次显示取消按钮时没有改变,但在第二次时改变了。

That is until I saw @polo987's answer. Here's what I did that worked:

直到我看到了@polo987的答案。我所做的是:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Done"

#8


3  

I have added titleLabel.font to the mix...

我有添加titleLabel。字体的组合…

This goes straight into my ViewDidLoad() {

这直接进入了我的ViewDidLoad() {

        let searchFOnt= UIFont(name: "BrandonGrotesque-Medium", size: 12)
        let textFieldSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
        textFieldSearchBar.fOnt= searchFont
        let cancelButton = searchBar.valueForKey("cancelButton") as! UIButton
        cancelButton.setTitle("Done", forState: .Normal)
        cancelButton.titleLabel!.fOnt= searchFont

#9


2  

I know this may seem to be a bit irrelevant but in my opinion this is safer than using private apis and more efficient than taking a dive into the unknown views. This solution makes sense only if you have your own localisation engine and you do want Apple to follow your mechanism all over the app. Basically my idea is to switch the language the iOS SDK uses to localise the button by altering the "AppleLanguages" key in the NSUserDefaults. You can go here for more information about how this works.

我知道这似乎有点无关紧要,但在我看来,这比使用私有api更安全,也比深入研究未知视图更有效。这个解决方案只有在你有自己的本地化引擎的情况下才有意义,而且你希望苹果在整个应用程序中都遵循你的机制。基本上,我的想法是通过修改NSUserDefaults中的“AppleLanguages”键来切换iOS SDK用于本地化按钮的语言。你可以到这里获取更多关于它是如何工作的信息。

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

Put this code to use the English localisation and a French fallback no matter what language is set on the phone. This only takes effect within the app.

将此代码用于使用英语本地化和法语后备,无论在电话上设置什么语言。这只会在应用中生效。

#10


2  

In Swift4

在Swift4

Change Title:

改变标题:

(searchBar.value(forKey: "cancelButton") as! UIButton).setTitle("Done", for: .normal)

(searchBar。值(forKey:“cancelButton”)!UIButton)。setTitle(“完成”:.normal)

Change Color:

改变颜色:

(searchBar.value(forKey: "cancelButton") as! UIButton).setTitleColor(UIColor.blue, for: .normal)

(searchBar。值(forKey:“cancelButton”)!UIButton).setTitleColor(用户界面颜色。蓝色:.normal)

#11


0  

Here is Swift solution:

这是迅速的解决方案:

for (view) in _searchController.searchBar.subviews {
    for (subview) in view.subviews {
        if (subview.isKindOfClass(UIButton.self)) {
            let cancelButton = subview as! UIButton
            cancelButton.setTitle("BlaBla", forState: .Normal)
            break
        }
    }
}

推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • vue使用
    关键词: ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • node.jsurlsearchparamsAPI哎哎哎 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
author-avatar
立案whan_597
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有