IOS改變UISearchBar中搜尋方塊的高度_IOS

來源:互聯網
上載者:User

一、系統的searchBar
1、UISearchBar的中子控制項及其布局
UIView(直接子控制項) frame 等於 searchBar的bounds,view的子控制項及其布局

  • UISearchBarBackground(間接子控制項) frame 等於searchBar的bounds
  • UISearchBarTextField(間接子控制項) frame.origin等於(8.0, 6.0),即不等於searchBar的bounds

2、改變searchBar的frame只會影響其中搜尋方塊的寬度,不會影響其高度,原因如下:

  • 系統searchBar中的UISearchBarTextField的高度預設固定為28
  • 左右邊距固定為8,上下邊距是父控制項view的高度減去28除以2

二、改變UISearchBar的高度
1、方案
重寫UISearchBar的子類(IDSearchBar),重新布局UISearchBar子控制項的布局
增加成員屬性contentInset,控制UISearchBarTextField距離父控制項的邊距

  • 若使用者沒有設定contentInset,則計算出預設的contentInset
  • 若使用者佈建了contentInset,則根據最新的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)        }      }    }  }}

三、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

// 設定contentInsetsearchBar.contentInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)

效果如圖

四、IDSearchBar的設計原則
1、注意

  • UISearchBar預設是有自己預設的布局方式的
  • 設計IDSearchBar旨在改變searBar中搜尋方塊的高度,但是可能會有改變寬的的需求

2、設計原則

  • 在沒有改變searchBar中搜尋方塊的高度的需求時,需要使用UISearchBar的預設布局
  • 若需要改變searchBar中搜尋方塊的高度的需求時,需要按照需求來改變UISearchBar的布局
  • 為了增加可控性,在IDSearchBar中增加成員屬性contentInset來控制IDSearchBar的內邊距

以上就是本文的全部內容,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.