iOS 11 的UISearchBar的处理

#undef macroIOS11_ORLATER
#define IOS11_ORLATER ([[[UIDevice currentDevice] systemVersion] compare:@"11.0" options:NSNumericSearch] != NSOrderedAscending )

@implementation MADSearchBar
{
    NSString *_cancelTitle;
    UIEdgeInsets _insets;
    UITextField *_searchField;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _setUpView];
    }
    return self;
}

#pragma mark - Private
- (void)_setUpView
{
    // 设置搜索图标
     [self setImage: [UIImage imageNamed:@"search"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    // iOS11版本以后 高度限制为44
    if (IOS11_ORLATER) {
        [self.heightAnchor constraintEqualToConstant:44].active = YES;
        self.searchTextPositionAdjustment = (UIOffset){10, 0}; // 光标偏移量
    }
    // 设置边距
    CGFloat top = 8;
    CGFloat bottom = top;
    CGFloat left = 12;
    CGFloat right = left;
    _insets = UIEdgeInsetsMake(top, left, bottom, right);
}

- (void)_hookSearchBar
{
    // 遍历子视图,获取输入UITextFiled
    if (!_searchField) {
        NSArray *subviewArr = self.subviews;
        for(int i = 0; i < subviewArr.count ; i++) {
            UIView *viewSub = [subviewArr objectAtIndex:i];
            NSArray *arrSub = viewSub.subviews;
            for (int j = 0; j < arrSub.count ; j ++) {
                id tempId = [arrSub objectAtIndex:j];
                if([tempId isKindOfClass:[UITextField class]]) {
                    _searchField = (UITextField *)tempId;
                }
            }
        }
    }
    
    if (_searchField) {
        //自定义UISearchBar
        UITextField *searchField = _searchField;
        if (IOS11_ORLATER) {
            // iOS11版本以后进行适配
            CGRect frame = searchField.frame;
            CGFloat offsetX = frame.origin.x - _insets.left;
            CGFloat offsetY = frame.origin.y - _insets.top;
            frame.origin.x = _insets.left;
            frame.origin.y = _insets.top;
            frame.size.height += offsetY * 2;
            frame.size.width += offsetX * 2;
            searchField.frame = frame;
        }
        // 自定义样式
        searchField.placeholder = @"请输入关键字";
        searchField.font = [UIFont systemFontOfSize:16];
        searchField.backgroundColor = [UIColor whiteColor];
        [searchField setBorderStyle:UITextBorderStyleNone];
        [searchField setTextAlignment:NSTextAlignmentLeft];
        [searchField setTextColor:[UIColor grayColor]];
        // 设置圆角
        searchField.layer.masksToBounds = YES;
        searchField.layer.cornerRadius = 5.0f;
       
        // 设置placeholder是否居中显示
        SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);
        
    
        if ([self respondsToSelector:centerSelector])
        {
            BOOL hasCentredPlaceholder = NO;
            NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
            [invocation setTarget:self];
            [invocation setSelector:centerSelector];
            [invocation setArgument:&hasCentredPlaceholder atIndex:2];
            [invocation invoke];
        }
    }
}
#pragma mark - Layout
-(void) layoutSubviews
{
    [super layoutSubviews];
    [self _hookSearchBar];
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值