[Object-c] UISearchBar 上"Cancel"按钮改为”取消“

来源:互联网 发布:逃脱者2mac中文版 编辑:程序博客网 时间:2024/06/05 23:03
目的:

修改 UISearchBar上"Cancel"按钮改为”取消“ 并且修改cancel按钮的颜色,字, 字体

理论:
    只有通过获取 searchBar 上所有view数据, 获取数组中第一个view,就是取消按钮.获取到按钮之后, 就能改变取消按钮的任何元素. 
代码实现:

for(UIView *viewin  [[[searchBar subviews]objectAtIndex:0]subviews]) {        if([viewisKindOfClass:[NSClassFromString(@"UINavigationButton")class]]) {           UIButton * cancel =(UIButton *)view;            [cancel setTitle:@"取消"forState:UIControlStateNormal];             //修改文字颜色            [cancel setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];            [cancel setTitleColor:[UIColorwhiteColor] forState:UIControlStateHighlighted];            // 修改按钮背景            [cancel setBackgroundImage:[UIImageimageNamed:@"btn_cancel.png"]forState:UIControlStateNormal];            [cancel setBackgroundImage:nilforState:UIControlStateHighlighted];        }    }



0 0