系统的searchBar


  • UISearchBar的中子控件及其布局

    • UIView(直接子控件) frame 等于 searchBar的bounds,view的子控件及其布局

      • UISearchBarBackground(间接子控件) frame 等于searchBar的bounds
      • UISearchBarTextField(间接子控件) frame.origin等于(8.0, 6.0),即不等于searchBar的bounds
  • 改变searchBar的frame只会影响其中搜索框的宽度,不会影响其高度,原因如下:

    • 系统searchBar中的UISearchBarTextField的高度默认固定为28
    • 左右边距固定为8,上下边距是父控件view的高度减去28除以2

改变UISearchBar的高度


  • 方案

    • 重写UISearchBar的子类(IDSearchBar),重新布局UISearchBar子控件的布局
    • 增加成员属性contentInset,控制UISearchBarTextField距离父控件的边距
      • 若用户没有设置contentInset,则计算出默认的contentInset
      • 若用户设置了contentInset,则根据最新的contentInset布局UISearchBarTextField
  • 具体实现

    • 重写UISearchBar的子类

      class IDSearchBar: UISearchBar {
      
      }
    • 增加成员属性contentInset(可选类型),控制UISearchBarTextField距离父控件的边距,监听其值的改变,重新布局searchBar子控件的布局

      var contentInset: UIEdgeInsets? {
      didSet {
      self.layoutSubviews()
      }
      }
    • 重写layoutSubviews()布局searchBar的子控件

      override func layoutSubviews() {
      super.layoutSubviews() // view是searchBar中的唯一的直接子控件
      for view in self.subviews {
      // UISearchBarBackground与UISearchBarTextField是searchBar的简介子控件
      for subview in view.subviews { // 找到UISearchBarTextField
      if subview.isKindOfClass(UITextField.classForCoder()) { if let textFieldContentInset = contentInset { // 若contentInset被赋值
      // 根据contentInset改变UISearchBarTextField的布局
      subview.frame = CGRect(x: textFieldContentInset.left, y: textFieldContentInset.top, width: self.bounds.width - textFieldContentInset.left - textFieldContentInset.right, height: self.bounds.height - textFieldContentInset.top - textFieldContentInset.bottom)
      } else { // 若contentSet未被赋值
      // 设置UISearchBar中UISearchBarTextField的默认边距
      let top: CGFloat = (self.bounds.height - 28.0) / 2.0
      let bottom: CGFloat = top
      let left: CGFloat = 8.0
      let right: CGFloat = left
      contentInset = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
      }
      }
      }
      }
      }

IDSearchBar使用示例


  • 未设置contentInset

    • 设置searchBar的frame

      searchBar.frame = CGRect(x: 80, y: 100, width: 200, height: 40)
    • 效果如图

  • 设置contentInset

    • 设置searchBar的frame

      searchBar.frame = CGRect(x: 80, y: 100, width: 200, height: 40)
    • 设置searchBar的contentInset

      // 设置contentInset
      searchBar.contentInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
    • 效果如图

IDSearchBar的设计原则


  • 注意

    • UISearchBar默认是有自己默认的布局方式的
    • 设计IDSearchBar旨在改变searBar中搜索框的高度,但是可能会有改变宽的的需求
  • 设计原则
    • 在没有改变searchBar中搜索框的高度的需求时,需要使用UISearchBar的默认布局
    • 若需要改变searchBar中搜索框的高度的需求时,需要按照需求来改变UISearchBar的布局
    • 为了增加可控性,在IDSearchBar中增加成员属性contentInset来控制IDSearchBar的内边距

UISearchBar改变搜索框的高度的更多相关文章

  1. iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)

    很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...

  2. ios UISearchBar搜索框的基本使用

    摘要: 小巧简洁的原生搜索框,漂亮而易用,如果我们的应用没有特殊需求,都可以使用它. iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建 ...

  3. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  4. iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  5. 快捷搜索框(UISearchBar)简单实现 swift

    1.在故事板里面拖入一个搜索栏和一个的tableView.2.创建的ViewController,实现代理:UISearchBarDelegate,UITableViewDataSource,的UIT ...

  6. iOS学习之NSPredictae及搜索框的实现

    NSPredicate Predicate 即谓词逻辑, Cocoa框架中的NSPredicate用于查询,作用是从数据堆中根据条件进行筛选.计算谓词之后返回的结果永远为BOOL类型的值,当程序使用谓 ...

  7. iOS搜索框

    在iOS8以前搜索框是作为一个控件添加到TableViewController中, 有系统自带的搜索变量self.searchDisplayController 遵守一个搜索显示的协议<UISe ...

  8. iOS 搜索框控件 最简单的dome

    刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...

  9. EasyUI基础searchbox&amp;progressbar(搜索框,进度条)

    easyui学习的基本组成部分(八个部分)硕果仅存searchbox和pargressbar.tooltip该,有一点兴奋.本文将偏向searchbox和pargressbar做一个探讨.鉴于双方的内 ...

随机推荐

  1. [Math] Beating the binary search algorithm – interpolation search, galloping search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  2. SQL Server窗口函数:ROWS与RANGE

    几乎每次我展示SQL Server里的窗口时,人们都非常有兴趣知道,当你定义你的窗口(指定的一组行)时,ROWS与RANGE选项之间的区别.因此在今天的文章里我想给你展示下这些选项的区别,对于你的分析 ...

  3. HT图形组件设计之道(一)

    HT for Web简称HT提供了涵盖通用组件.2D拓扑图形组件以及3D引擎的一站式解决方案,正如Hightopo官网所表达的我们希望提供:Everything you need to create ...

  4. MagicalRecord,一个简化CoreData操作的工具库

    简介 项目主页:https://github.com/magicalpanda/MagicalRecord 实例下载:https://github.com/ios122/MagicalRecord 在 ...

  5. 《构建之法》第8、9、10章读书笔记、读后感以及Sprint1总结

    第八章:需求分析 软件需求 人们(用户)的需求五花八门,作为一个软件团队要准确而全面地获取这些需求主要有以下四个步骤: 获取和引导需求.这一步骤也被叫做“需求捕捉”.软件团队需要为用户着想,设身处地, ...

  6. ref和out 传递参数(C#)

    1.参数传递默认都是传递栈空间里面存储的内容 2.如果添加了ref那么传递的都是栈空间地址,而不再是栈空间里面的内容 3.如果添加了out,那么传递的也是栈空间的地址 //写一个方法计算一个int类型 ...

  7. STL or 线段树 --- CSU 1555: Inversion Sequence

    Inversion Sequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一 ...

  8. Ubuntu搭建Android交叉编译环境

    一.下载 Android NDK Android NDK官方下载页:http://developer.android.com/tools/sdk/ndk/index.html如果需要旧版本的,比如10 ...

  9. 创建一个带模版的用户控件 V.3

    再重构此篇<创建一个带模版的用户控件  V.2>http://www.cnblogs.com/insus/p/4164149.html 让其它动态实现header,Item和Footer. ...

  10. 重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress

    [源码下载] 重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithPro ...