iOS开发-搜索栏 UISearchController

在这里就不细节的描述UISearchController的所有属性和方法了,主要提一下遇到的一些坑以及微信搜索框的实现方法之一:

进入编辑状态时会出现偏移64点的问题,设置下面的属性即可:
self.definesPresentationContext =YES;

当然啦,加了这个后,当属性hidesNavigationBarDuringPresentation = YES时有人碰到搜索框进入编辑时向下偏移64的问题 ,设置 navigationBar 的 translucent属性即可,这个属性默认是NO,需要设置为YES:
[self.navigationController.navigationBarsetTranslucent:YES];

这样就完美解决了 。

接下来说个实例,仿微信搜索框:

大家都知道微信在拉人进群,选择的人会显示在搜索框的左侧,其实这个做法有很多种,我接下来我所实现的方法:
不知道大家有没有仔细看UITextField的属性,它有一个leftView,就是那个放大镜,这个是可以自定义的
拿到searchBar的TextField
UITextField *searchField = [self.searchController.searchBarvalueForKey:@"_searchField"];
可以先申明个属性保存这个放大镜,像微信那样在合适的时候换成放大镜
self.lensView = searchField.leftView;
我用的是UIColectionView来做leftView,UIColectionView的创建我这里就不说了,其实就是在合适的时候,放大镜和UIColectionView赋给leftView,在选择人的时候进行判断

if (self.seleArray.count > 0) {
        searchField.leftView = self.collectionView;
    } else {
        searchField.leftView = self.lensView;
    }

记得改变UICollectionView的宽。
按照微信的显示,如果没选择人,进入编辑时要隐藏放大镜,退出编辑时要显示放大镜,这个可以在UISearchBarDelegate里进行设置:

#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    //隐藏放大镜
    UITextField *searchField = [searchBar valueForKey:@"_searchField"];
    if (searchField.leftView == self.lensView) {
        [searchField setLeftViewMode:UITextFieldViewModeNever];
    } else {
        [searchField setLeftViewMode:UITextFieldViewModeAlways];
    }
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    UITextField *searchField = [searchBar valueForKey:@"_searchField"];
    [searchField setLeftViewMode:UITextFieldViewModeAlways];
}

删除人和增加人就相当于删除和增加UICollectionView的cell,也很方便,但是我在这里遇到问题,就是UICollectionView作为leftView时,cell不能被选中,这个问题我没找到原因,实现微信点击cell删除功能,我是在cell上的UIImageView上设置点击手势,利用手势来达到点击cell的效果,如果大家有更好的实现方法,可以留言,谢谢!

另外一个点就是放大镜默认居左显示问题,这个在网上有很多方法,具体就是建一个UISearchBar的分类,用一个属性来控制是居中还是居左显示,这里就不多做讲述了,直接上代码吧:

h文件
#import <UIKit/UIKit.h>

@interface UISearchBar (LeftPlaceholer)

//YES 居中
//NO 靠左
@property (nonatomic,assign,setter = setHasCenterdPlaceholder:)BOOL hasCenterdPlaceholder;

@end
m文件
#import "UISearchBar+LeftPlaceholer.h"

@implementation UISearchBar (LeftPlacdholer)
@dynamic hasCenterdPlaceholder;
- (void)setHasCenterdPlaceholder:(BOOL)hasCenterdPlaceholder {
    hasCenterdPlaceholder = hasCenterdPlaceholder;
    SEL centerSelector =NSSelectorFromString([NSStringstringWithFormat:@"%@%@",@"setCenter",@"Placeholder:"]);
    if ([selfrespondsToSelector:centerSelector]) {
        NSMethodSignature *signature = [[UISearchBarclass]instanceMethodSignatureForSelector:centerSelector];
        NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:signature];
        [invocation setTarget:self];
        [invocation setSelector:centerSelector];
        [invocation setArgument:&hasCenterdPlaceholderatIndex:2];
        [invocation invoke];
    }
}

@end

第一次写,全是干货,希望能帮助到大家,谢谢!

效果图

仿微信搜索框效果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值