facebook twitter hatena line email

Ios/swift/SearchBar

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

SearchBarとは

検索ボックスです

検索バーサンプル

import UIKit
class SearchViewController: UIViewController, UISearchBarDelegate {
   var mySearchBar: UISearchBar!
   private var _searchText: String = ""
   override func viewDidLoad() {
       super.viewDidLoad()
       mainSearchBar.delegate = self
       mainSearchBar.placeholder = "検索"
   }
   // 検索バー更新時
   func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
       _searchText = searchText
   }
   // キャンセル
   func searchBarCancelButtonClicked(searchBar: UISearchBar) {
       _searchText = ""
   }
   // 検索ボタン
   func searchBarSearchButtonClicked(searchBar: UISearchBar) {
       mainSearchBar.text = ""
       self.view.endEditing(true)
       // searchText
   }
}

SearchBarをコードで生成する場合

mainSearchBar = UISearchBar()
mainSearchBar.delegate = self
mainSearchBar.frame = CGRectMake(0, 0, 300, 50)
mainSearchBar.layer.position = CGPoint(x: self.view.bounds.width/2, y: 50)
mainSearchBar.showsCancelButton = true
mainSearchBar.placeholder = "検索"
mainSearchBar.text = "テスト"
self.view.addSubview(mainSearchBar)