IOS改變UISearchBar中搜索框的高度
一、系統(tǒng)的searchBar
1、UISearchBar的中子控件及其布局
UIView(直接子控件) frame 等于 searchBar的bounds,view的子控件及其布局
- UISearchBarBackground(間接子控件) frame 等于searchBar的bounds
- UISearchBarTextField(間接子控件) frame.origin等于(8.0, 6.0),即不等于searchBar的bounds
2、改變searchBar的frame只會影響其中搜索框的寬度,不會影響其高度,原因如下:
- 系統(tǒng)searchBar中的UISearchBarTextField的高度默認固定為28
- 左右邊距固定為8,上下邊距是父控件view的高度減去28除以2
二、改變UISearchBar的高度
1、方案
重寫UISearchBar的子類(IDSearchBar),重新布局UISearchBar子控件的布局
增加成員屬性contentInset,控制UISearchBarTextField距離父控件的邊距
- 若用戶沒有設(shè)置contentInset,則計算出默認的contentInset
- 若用戶設(shè)置了contentInset,則根據(jù)最新的contentInset布局UISearchBarTextField
2、具體實現(xiàn)
重寫UISearchBar的子類
class IDSearchBar: UISearchBar { }
增加成員屬性contentInset(可選類型),控制UISearchBarTextField距離父控件的邊距,監(jiān)聽其值的改變,重新布局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被賦值 // 根據(jù)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未被賦值 // 設(shè)置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使用示例
1、未設(shè)置contentInset
設(shè)置searchBar的frame
searchBar.frame = CGRect(x: 80, y: 100, width: 200, height: 40)
效果如圖
2、設(shè)置contentInset
設(shè)置searchBar的frame
searchBar.frame = CGRect(x: 80, y: 100, width: 200, height: 40)
設(shè)置searchBar的contentInset
// 設(shè)置contentInset searchBar.contentInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
效果如圖
四、IDSearchBar的設(shè)計原則
1、注意
- UISearchBar默認是有自己默認的布局方式的
- 設(shè)計IDSearchBar旨在改變searBar中搜索框的高度,但是可能會有改變寬的的需求
2、設(shè)計原則
- 在沒有改變searchBar中搜索框的高度的需求時,需要使用UISearchBar的默認布局
- 若需要改變searchBar中搜索框的高度的需求時,需要按照需求來改變UISearchBar的布局
- 為了增加可控性,在IDSearchBar中增加成員屬性contentInset來控制IDSearchBar的內(nèi)邊距
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
iOS通過Runtime實現(xiàn)友盟統(tǒng)計的實例代碼
本篇文章主要介紹了iOS通過Runtime實現(xiàn)友盟統(tǒng)計的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06總結(jié)iOS App開發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式
這篇文章主要介紹了iOS app開發(fā)中控制屏幕旋轉(zhuǎn)的方法總結(jié),分為自動旋轉(zhuǎn)和手動旋轉(zhuǎn)以及強制旋轉(zhuǎn)三種情況,代碼為Objective-C語言,需要的朋友可以參考下2016-02-02iOS應(yīng)用開發(fā)中運用設(shè)計模式中的組合模式的實例解析
這篇文章主要介紹了iOS應(yīng)用開發(fā)中運用設(shè)計模式中的組合模式的實例解析,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03實例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10針對iOS開發(fā)的一些Xcode使用技巧小結(jié)
這篇文章主要介紹了針對iOS開發(fā)的一些Xcode使用技巧小結(jié),Xcode是Mac上編寫iOS應(yīng)用的開發(fā)環(huán)境,需要的朋友可以參考下2015-12-12詳解IOS UITableViewCell 的 imageView大小更改
這篇文章主要介紹了詳解IOS UITableViewCell 的 imageView大小更改的相關(guān)資料,需要的朋友可以參考下2017-07-07