ios 改变searchBar的高度

最近手机更新了iOS11系统,把项目在手机上运行之后发现原来正常大小的searchBar高度明显增加了,放在导航栏上更为明显。就想着要把searchBar的高度改一下,在网上搜了一下全都一样,而且还是用swift写的,所以就想着按照那个方法写一个OC版本的(参考文章地址:www.cnblogs.com/theDesertIslandOutOfTheWorld/p/5015653.html)。

ios 改变searchBar的高度_第1张图片
iOS11系统的searchBar

ios 改变searchBar的高度_第2张图片
iOS10.3系统的searchBar

具体的UISearchBar的中子控件及其布局可以参考原文章的分析,现在直接上代码

新建UISearchBar的子类,增加成员属性contentInset,用来调整UISearchBarTextField距离父控件的边距。contentInset的setter方法

#pragma mark - setter method
- (void)setContentInset:(UIEdgeInsets)contentInset {
   
   _contentInset.top = contentInset.top;
   _contentInset.bottom = contentInset.bottom;
   _contentInset.left = contentInset.left;
   _contentInset.right = contentInset.right;
   
   self.isChangeFrame = YES;
   [self layoutSubviews];
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    for (UIView *subView in self.subviews[0].subviews) {
        
        if ([subView isKindOfClass:[UIImageView class]]) {
            
            //移除UISearchBarBackground
            [subView removeFromSuperview];
        }
        if ([subView isKindOfClass:[UITextField class]]) {
            
            CGFloat height = self.bounds.size.height;
            CGFloat width = self.bounds.size.width;
            
            if (_isChangeFrame) {
                //说明contentInset已经被赋值
                // 根据contentInset改变UISearchBarTextField的布局
                subView.frame = CGRectMake(_contentInset.left, _contentInset.top, width - 2 * _contentInset.left, height - 2 * _contentInset.top);
            } else {
                
                // contentSet未被赋值
                // 设置UISearchBar中UISearchBarTextField的默认边距
                CGFloat top = (height - 28.0) / 2.0;
                CGFloat bottom = top;
                CGFloat left = 8.0;
                CGFloat right = left;
                _contentInset = UIEdgeInsetsMake(top, left, bottom, right);
            }
        }
    }
}

使用

- (MySelfSearchBar *)addSearchBarWithFrame:(CGRect)frame {
    
    self.definesPresentationContext = YES;
    
    MySelfSearchBar *searchBar = [[MySelfSearchBar alloc]initWithFrame:frame];
    searchBar.delegate = self;
    searchBar.searchBarStyle = UISearchBarStyleDefault;
    searchBar.placeholder = @"查询";
    [searchBar setShowsCancelButton:NO];
    [searchBar setTintColor:KGenericColor];
    
    if (self.isChangeSearchBarFrame || IS_IOS_11) {
        
        CGFloat height = searchBar.bounds.size.height;
        CGFloat top = (height - 20.0) / 2.0;
        CGFloat bottom = top;
        
        searchBar.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    }
    
    return searchBar;
}

加载到导航栏上

    MySelfSearchBar *searchBar = [self addSearchBarWithFrame:CGRectMake(0, 0, kScreenWidth - 2 * 44 - 2 * 15, 44)];
    UIView *wrapView = [[UIView alloc] initWithFrame:searchBar.frame];
    [wrapView addSubview:searchBar];
    self.navigationItem.titleView = wrapView;

通过新增属性contentInset来改变searchBar的高度
运行结果

ios 改变searchBar的高度_第3张图片
改变与父控件的边距后效果

demo地址:https://github.com/DreamTravelingLight/searchBarDemo.git
以前的源码没了,就简单的写了一下,明白了原理可以根据自己的项目再写一个

你可能感兴趣的:(ios 改变searchBar的高度)