ios 改变searchBar的高度

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

iOS11系统的searchBar

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的高度
运行结果

改变与父控件的边距后效果

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,202评论 4 61
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,086评论 3 38
  • 文/亮妈 2016年最后一天了,大家都在哪里做什么呢?五福集齐了吗?本来计划在公历2016年底写的年度总结拖到了农...
    亮妈Maggie阅读 498评论 0 1
  • 2016年国庆节前夕,我为父母办了一场素食感恩宴,源自于这一年多来的学习,因为我是本地一家免费互助素食餐厅的发起人...
    章益华的日常分享阅读 701评论 0 1
  • 1.人物 合理。出场退场。语言,思维,行为,情感。如果一定要有悖常理,事先铺垫。 人物分类 地位,文化,经济...
    胖妞兰阅读 311评论 0 0