之前看了 Ray Wenderlich 的一篇文章 學習了一下怎么把 UISearchBar 放在 UITableView 的頭部击困,來進行列表搜索,然后發(fā)現(xiàn)了兩個問題,用了好久才解決:
- UISearchBar 有個 1 像素寬的黑色邊框,去不掉
- 把 UISearchBar 放在 UITableView 頭部的代碼在 iOS 8 不起作用
tableView.tableHeaderView = searchController.searchBar // now working
解決問題 1
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = // your background color
解決問題 2
我發(fā)現(xiàn) tableHeaderView 是可以被其他 view 有效填充的征候,不知道為什么 searchBar 就是放不進去,所以直接找個大小一樣的 UIView 作為容器放在 searchBar 下面就好了祟敛。
let containerView = UIView(frame: CGRectMake(0, 64, view.frame.width, 44))
containerView.backgroundColor = // your background color
searchController.searchBar.frame = CGRectMake(0, 0, view.frame.width, 44)
containerView.addSubview(searchController.searchBar)
tableView.tableHeaderView = containerView