首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >修改UISearchBar取消按钮字体、文本颜色和样式

修改UISearchBar取消按钮字体、文本颜色和样式
EN

Stack Overflow用户
提问于 2012-07-20 11:18:30
回答 8查看 24.2K关注 0票数 21

有没有办法更改UISearchBar取消按钮的文本字体和颜色,而不需要创建搜索栏的子类化?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2012-07-20 12:21:57

UIBarButtonItem包含在UISearchBar中时,可以通过更改其外观来更改取消按钮样式。

例如,

代码语言:javascript
复制
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor blueColor], 
                                                      UITextAttributeTextColor, 
                                                      [UIColor darkGrayColor], 
                                                      UITextAttributeTextShadowColor, 
                                                      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
                                                      UITextAttributeTextShadowOffset,
                                                      nil] 
                                            forState:UIControlStateNormal];
票数 73
EN

Stack Overflow用户

发布于 2016-12-08 22:54:35

Swift 3

代码语言:javascript
复制
let attributes = [
    NSForegroundColorAttributeName : UIColor.white,
    NSFontAttributeName : UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
票数 19
EN

Stack Overflow用户

发布于 2014-02-23 11:10:33

根据htinlinn的回答,这是我在iOS 7中使用搜索栏的视图控制器的viewDidLoad方法中使用的方法:

代码语言:javascript
复制
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
                     setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] 
                                   forState:UIControlStateNormal];
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11572372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档