ios7 左右searchbar在设置cancelButton的title属性

经 [searchBarsetShowsCancelButton:YES];设置之后默认是“cancel”,想改为中文的

在stackoverflow上參考了非常多方法都没用

这里的关键是要获取到 searchbar中得 cancelButton

通过打印 [searchbar subviews]

得到结果例如以下

(lldb) po [searchBar subviews]

<__NSArrayM 0xd6b0db0>(

<UIView: 0xd6aab30; frame = (0 0; 240 24); clipsToBounds = YES; autoresize =

+H; layer = <CALayer: 0xd6aab90>>

)

也就是其仅仅有一个subview,在stackoverflow上得到些事实上

这个cancelbutton在更深一层

打印

[[[searchBar subviews] objectAtIndex:0] subviews]

得到结果:

(lldb) po [[[searchBar subviews] objectAtIndex:0] subviews]

<__NSArrayM 0xd6b0dd0>(

<UISearchBarBackground: 0xd6ab370; frame = (0 0; 240 24); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xd6ab4c0>>,

<UINavigationButton: 0xd6abef0; frame = (0 0; 54 30); opaque = NO; layer = <CALayer: 0xd6ac020>>,

<UISearchBarTextField: 0xd6acbe0; frame = (0 0; 0 0); text = ‘‘; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0xd6acdf0>>

)

也就是这个UINavigationButton。对它进行设置

代码例如以下

ios6和ios7情况不一样,ios7中searchBar得层级多了一层

    if (iOS7) {
        for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
            if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
                UIButton * cancel =(UIButton *)view;
                [cancel setTitle:@"取消" forState:UIControlStateNormal];
                [cancel  setTintColor:[UIColor blackColor]];
                [cancel.titleLabel setTextColor:[UIColor blackColor]];
                //            [cancel setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
            }
            if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                [view removeFromSuperview];
            }

        }
    }else
    {
        for(UIView *view in  [searchBar subviews]) {
            if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
                UIButton * cancel =(UIButton *)view;
                [cancel setTitle:@"取消" forState:UIControlStateNormal];
                [cancel  setTintColor:[UIColor blackColor]];
                [cancel.titleLabel setTextColor:[UIColor blackColor]];
                cancel.backgroundColor = [UIColor clearColor];
            }
            if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                [view removeFromSuperview];
            }

        }
    }

能够通过

(lldb) po [searchBar recursiveDescription]  来查看一下searchBar的视图层级

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-08-03 01:29:24

ios7 左右searchbar在设置cancelButton的title属性的相关文章

ios7 关于searchbar上设置cancelButton的title属性

通过 [searchBarsetShowsCancelButton:YES];设置之后默认是"cancel",想改为中文的 在stackoverflow上参考了很多方法都没用 这里的关键是要获取到 searchbar中得 cancelButton 通过打印 [searchbar subviews] 得到结果如下 (lldb) po [searchBar subviews] <__NSArrayM 0xd6b0db0>( <UIView: 0xd6aab30; fram

jquery - 设置/获取内容和属性

一般我们会遇到给某个元素添加或更改原有的文字: 1. 设置/获取内容 - text().html() 以及 val() 设置内容常用的三个方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - 设置或返回表单字段的值 eg: 通过 text().html() 以及 val() 方法来设置/获取内容: $("#btn1").click(function(){ $("#test1").t

如何设置backBarButtonItem的title和action

1.为什么在UIViewController内设置了self.navigationItem.backBarButtonItem 对于导航栏显示的backBarButtonItem不起任何作用? 首先我们得了解一下 backBarButtonItem(其中当text属性太长时就会显示失败) leftBarButtonItem rightBarButtonItem 他们都属于UINavigationItem的组成部分,都显示在navigationBar上,都属于UIBarButtonItem类 ba

使用V7包后,设置ActionBar的title等的属性

最新在做一个项目,既要支持低版本的ActionBar,又要能设置ActionBar的背景,及title的颜色. 使用V7包后,Activity只能设置固定的三个Theme. @style/Theme.AppCompat , @style/Theme.AppCompat.Light., @style/Theme.AppCompat.Light.DarkActionBar 如果,此时,我加了一个绿色的ActionBar的背景,又想title的颜色是白色. int titleId = Resource

【CSS进阶】伪元素的妙用2 - 多列均匀布局及title属性效果

最近无论是工作还是自我学习提升都很忙,面对长篇大论的博文总是心有余而力不足,但又不断的接触学习到零碎的但是很有意义的知识点,很想分享给大家,所以本篇可能会很短. 本篇接我另一篇讲述 CSS 伪元素的文章: [CSS进阶]伪元素的妙用–单标签之美,看完本文觉得有意思的可以再去看看上一篇,分享了一些伪元素的妙用. 正文从这里开始: 哪些标签不支持伪元素? 我也是才知道这个姿势.为了不误导读者,就赶紧补充一下. 伪元素虽然强大,但是还是有一些特定的标签是不支持伪元素 before 和 after 的.

agruments应用——求出函数参数的总合&amp;&amp;css函数——设置/读取对象的属性&amp;&amp;当前输入框高亮显

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

设置UINavigationController标题的属性

self.title = @"产品详情"; [self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:20.f], NSFontAttributeName, nil]]; 可以

html基础 设置img的align属性,left,right 实现图文混排的效果

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ ex1: code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; chars

图片的title属性和alt属性的区别

在前端开发中,经常遇到有人在问图片的alt属性和title属性的区别,这是因为很多人对于alt属性和title属性没有彻底搞明白,今天零度给大家分析分析. title属性 首先,来看一下什么是title属性,在我们平时浏览网页的时候,我们经常会看到,当把鼠标放在一个文字链接上时,会弹出一段说明信息,这种提示信息就是通过title属性设置的.而不光文字链接有title属性,图片也可以设置title属性,title属性的作用就是提示的作用. 所以图片的title属性是提示作用,而这个提示作用是不管图