SearchBar简单展示
2017-04-01 16:52 jhs_哈哈 阅读(230) 评论(0) 编辑 收藏 举报import UIKit
class SearchViewController: UIViewController,UISearchBarDelegate {
let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
var topSearchBar:UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
//初始化searchBar
let topSearchBar = UISearchBar(frame: CGRect(x: 10, y: 5, width:SCREEN_WIDTH-80, height: 34))
//移除搜索框的灰色背景
for sv in topSearchBar.subviews {
if sv.isKind(of: NSClassFromString("UIView")!) && sv.subviews.count>0{
sv.subviews.first!.removeFromSuperview()
}
}
topSearchBar.placeholder = "搜索商城商品 "
//searchBar 协议
topSearchBar.delegate = self
//设置searchBar 边框宽度
topSearchBar.layer.borderWidth = 1
//设置searchBar 边框颜色
topSearchBar.layer.borderColor = UIColor(red: 212/255, green: 212/255, blue: 212/255, alpha: 1.0).cgColor
//搜索框父view
let searchView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_HEIGHT, height: 44))
searchView.backgroundColor = UIColor.clear
//searchBar放置到父view上面
searchView.addSubview(topSearchBar)
//view放到navigationItem主题上面
self.navigationItem.titleView = searchView
}
}