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 

@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 ()
{
    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

你可能感兴趣的:(iOS开发:使用KVC改变UISearchBar外观)