iOS开发:使用KVC改变UISearchBar外观

前言

KVC在之前的文章中已经详细介绍,这里不再多讲,今天主要讲讲使用KVC是如何改变UISearchBar的外观。

UISearchBar简述

我们在UISearchController或者是UISearchDisplayController中都可以直接获取到UISearchBar的实例,我们可以从这里改变一些UISearchBar的属性来改变外观显示。同时我们也可以直接获取UISearchBar的subViews,UISearchBar的subView是一个UIView的实例,这个UIView包含了所有在UISearchBar上可以展示的子视图,iOS SDK提供的UISearchBar,在iOS7之前是分为UISearchBarBackground、UISearchBarTextField、UIButton这几个类的实例组成,而在iOS7之后,是将UIButton转换为了UINavigationButton的实例。

1、我们可以通过循环遍历出UISearchBar上所有展示出来的子视图

for (UIView *view in [[[_searchBar subviews] lastObject] subviews]) {
        
    if([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){}
        
    if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {}
        
    if([view isKindOfClass:NSClassFromString(@"UINavigationButton")]) {}
}

2、通过KVC获取子视图

UIView *backgroundView = [_searchBar valueForKey:@"_background"];
    
UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    
UIButton *cancelButton = [_searchBar valueForKey:@"_cancelButton"];

3、当我们获取cancelButton时,一定要确保cancelButton包含在了UISearchBar中,必要时可以提前调用:

[_searchBar setShowsCancelButton:YES animated:NO];

4、去掉搜索框背景

for(UIView *view in[[[_searchBar subviews] lastObject] subviews]) {
        
    if([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            
        [view removeFromSuperview];
    }
}

5、去掉搜索框边框

[_searchBar setBackgroundImage:[UIImage new]];

6、改变输入框文本

//提示文本颜色
UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    
[searchField setTextColor:[UIColor blackColor]];
    
[searchField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    
[searchField setFont:[UIFont systemFontOfSize:14]];
    
[searchField setBackgroundColor:[UIColor whiteColor]];

7、改变取消按钮的title

UIButton *cancelButton = [_searchBar valueForKey:@"_cancelButton"];
    
[cancelButton setTitle:@"Close" forState:UIControlStateNormal];

示例代码

//
//  KVCSearchBarVC.h
//  KVC-KVO
//
//  Created by Jason on 2018/4/11.
//  Copyright © 2018年 hzb. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface KVCSearchBarVC : UIViewController

@end
//
//  KVCSearchBarVC.m
//  KVC-KVO
//
//  Created by Jason on 2018/4/11.
//  Copyright © 2018年 hzb. All rights reserved.
//

#import "KVCSearchBarVC.h"

@interface KVCSearchBarVC ()<UISearchBarDelegate>
{
    UISearchBar *_searchBar;
}

@end

@implementation KVCSearchBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //搜索框
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, kNavBarH+20, WIDTH-20, 44)];
    _searchBar.delegate = self;
    _searchBar.searchBarStyle = UISearchBarStyleMinimal;
    _searchBar.showsCancelButton = YES;
    _searchBar.placeholder = @"KVC、UISearchBar";
    [self.view addSubview:_searchBar];
    
    //通过循环遍历出UISearchBar上所有展示出来的子视图
    [self getSubviewByEach];

    //通过KVC获取子视图
    [self getSubviewByKvc];

    //当我们获取cancelButton时,一定要确保cancelButton包含在了UISearchBar中,必要时可以提前调用:
    [_searchBar setShowsCancelButton:YES animated:NO];

    //去掉搜索框背景
    [self dismissSearchBarBackground];

    //去掉搜索框边框
    [self dismissSearchBarLayer];

    //改变输入框的样式
    [self changeSearchFieldStyle];

    //改变取消按钮的样式
    [self changeCancelBtnStyle];
}

#pragma mark - 通过循环遍历出UISearchBar上所有展示出来的子视图
- (void)getSubviewByEach {
    for (UIView *view in [[[_searchBar subviews] lastObject] subviews]) {
        
        if([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){
            NSLog(@"UISearchBarBackground");
        }
        
        if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
            NSLog(@"UISearchBarTextField");
        }
        
        if([view isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            NSLog(@"UINavigationButton");
        }
    }
}

#pragma mark - 通过KVC获取子视图
- (void)getSubviewByKvc {
    UIView *backgroundView = [_searchBar valueForKey:@"_background"];
    
    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    
    UIButton *cancelButton = [_searchBar valueForKey:@"_cancelButton"];
}

#pragma mark - 去掉搜索框背景
- (void)dismissSearchBarBackground {
    for(UIView *view in[[[_searchBar subviews] lastObject] subviews]) {
        
        if([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            
            [view removeFromSuperview];
        }
    }
}

#pragma mark - 去掉搜索框边框
- (void)dismissSearchBarLayer {
    [_searchBar setBackgroundImage:[UIImage new]];
}

#pragma mark - 改变输入框的样式
- (void)changeSearchFieldStyle {
    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    
    [searchField setTextColor:[UIColor blackColor]];
    
    [searchField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    [searchField setFont:[UIFont systemFontOfSize:14]];
    
    [searchField setBackgroundColor:[UIColor whiteColor]];
}

#pragma mark - 改变取消按钮的样式
- (void)changeCancelBtnStyle {
    UIButton *cancelButton = [_searchBar valueForKey:@"_cancelButton"];
    
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    
    [cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

源码地址:https://github.com/hanzhanbing/KVC-KVO

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 在iOS8中,Apple在UIKit框架中给我们提供了UISearchController来代替之前的UIS...
    SeraZheng阅读 3,382评论 12 23
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,981评论 1 180
  • 「我找一個歪果仁朋友幫我搞定了。」 前幾日,就這麼一條信息,後面還跟著能恰好表達出C的心情的得意笑臉emoji,嚇...
    KrisKeely阅读 173评论 0 0
  • 剧情回顾:卜算子08 给我一个理由先 第09章:可是哥依然期待 有一种哀伤叫做哀莫大于心死,可是还有一种比哀伤更哀...
    兰十三阅读 518评论 0 0
  • 多线程有几种实现方式?如果被问到这个问题一定很头疼,因为百度一下随便就能出现各种各样的答案。两种、三种、四种、五种...
    徐志毅阅读 14,990评论 2 10

友情链接更多精彩内容