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的视图层级




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值