1、體系的searchBar
1、UISearchBar的中子控件及其結構
UIView(直接子控件) frame 等於 searchBar的bounds,view的子控件及其結構
2、轉變searchBar的frame只會影響個中搜刮框的寬度,不會影響其高度,緣由以下:
2、轉變UISearchBar的高度
1、計劃
重寫UISearchBar的子類(IDSearchBar),從新結構UISearchBar子控件的結構
增長成員屬性contentInset,掌握UISearchBarTextField間隔父控件的邊距
2、詳細完成
重寫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)
}
}
}
}
}
3、IDSearchBar應用示例
1、未設置contentInset
設置searchBar的frame
searchBar.frame = CGRect(x: 80, y: 100, width: 200, height: 40)
後果如圖

2、設置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)
後果如圖

4、IDSearchBar的設計准繩
1、留意
2、設計准繩
以上就是本文的全體內容,願望對年夜家的進修有所贊助。
【IOS轉變UISearchBar中搜刮框的高度】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!