UISearchBar改变搜索框的高度
系统的searchBar
UISearchBar的中子控件及其布局
- UIView(直接子控件) frame 等于 searchBar的bounds,view的子控件及其布局
- UISearchBarBackground(间接子控件) frame 等于searchBar的bounds
- UISearchBarTextField(间接子控件) frame.origin等于(8.0, 6.0),即不等于searchBar的bounds
- UIView(直接子控件) frame 等于 searchBar的bounds,view的子控件及其布局
改变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改变搜索框的高度的更多相关文章
- iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)
很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...
- ios UISearchBar搜索框的基本使用
摘要: 小巧简洁的原生搜索框,漂亮而易用,如果我们的应用没有特殊需求,都可以使用它. iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建 ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- 快捷搜索框(UISearchBar)简单实现 swift
1.在故事板里面拖入一个搜索栏和一个的tableView.2.创建的ViewController,实现代理:UISearchBarDelegate,UITableViewDataSource,的UIT ...
- iOS学习之NSPredictae及搜索框的实现
NSPredicate Predicate 即谓词逻辑, Cocoa框架中的NSPredicate用于查询,作用是从数据堆中根据条件进行筛选.计算谓词之后返回的结果永远为BOOL类型的值,当程序使用谓 ...
- iOS搜索框
在iOS8以前搜索框是作为一个控件添加到TableViewController中, 有系统自带的搜索变量self.searchDisplayController 遵守一个搜索显示的协议<UISe ...
- iOS 搜索框控件 最简单的dome
刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...
- EasyUI基础searchbox&progressbar(搜索框,进度条)
easyui学习的基本组成部分(八个部分)硕果仅存searchbox和pargressbar.tooltip该,有一点兴奋.本文将偏向searchbox和pargressbar做一个探讨.鉴于双方的内 ...
随机推荐
- 【转】github上值得关注的前端项目
综合/资源 frontend-dev-bookmarks 一个巨大的前端开发资源清单.star:15000 front-end-collect 分享自己长期关注的前端开发相关的优秀网站.博客.以及活跃 ...
- bootstrap插件学习-bootstrap.collapse.js
先看bootstrap.collapse.js的结构 var Collapse = function ( element, options ){} // 构造器 Collapse.prototype ...
- UML系列05之 基本流程图
概要 软件的基本流程图是我们在学习编程时的必修课,它很简单,却很实用.需要说明的是,UML并不包括软件的基本流程图,但是为了方便我自己查阅,所以将基本软件流程图归纳到UML系列当中.读者切不要认为基本 ...
- How to implement an algorithm from a scientific paper
Author: Emmanuel Goossaert 翻译 This article is a short guide to implementing an algorithm from a scie ...
- I'm back for Machine Learning
Hi, Long time no see. Briefly, I plan to step into this new area, data analysis. In the past few yea ...
- Java集合框架源码剖析:LinkedHashSet 和 LinkedHashMap
Java LinkedHashMap和HashMap有什么区别和联系?为什么LinkedHashMap会有着更快的迭代速度?LinkedHashSet跟LinkedHashMap有着怎样的内在联系?本 ...
- 全网最详系列之-倍增求LCA
1,什么是LCA LCA.最近公共祖先.是一个在解决树上问题最强劲有力的一个工具.一般都是指.在一棵树上取两个节点a,b .另一个节点x它满足 x是a与b的祖先而且x深度最大.这个x就是节点a,b的 ...
- HTTP请求响应报文&&相关状态码&&GET_POST请求方法 总结
HTTP请求报文: 一个HTTP请求报文由四个部分组成:请求行.请求头部.空行.请求数据 1.请求行 请求行由请求方法字段.URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔.比如 GE ...
- html5人物图片360度立体旋转
体验效果:http://hovertree.com/texiao/html5/10.htm 下载:http://hovertree.com/hvtart/bjae/t16oddyt.htm 代码如下: ...
- Python基础:模块
一.概述 二.导入语句 1.基本语法 2.推荐风格 三.模块 1.模块名 2.模块属性 3.可导出的公有属性 4.直接执行 四.包 1.包名 2.包属性 3.可导出的公有属性 4.其他 五.导入原理 ...