我的应用程序中有一个搜索视图,该视图可以正常工作。我需要更新搜索视图,以便在搜索栏为空时视图显示搜索类别。
如何在XLPagerTabStrip控制器上显示视图,同时仍将搜索栏保持在适当的位置?
class SearchViewController: ButtonBarPagerTabStripViewController, UISearchBarDelegate{
var businesses = [Business]()
var coupons = [Coupon]()
var businessSearchVC: BusinessSearchViewController?
var couponSearchVC: CouponSearchViewController?
lazy var searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Search businesses or coupons"
sb.barTintColor = .gray
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.mainWhite()
sb.delegate = self
return sb
}()
override func viewDidLoad() {
settings.style.buttonBarItemBackgroundColor = .clear
settings.style.selectedBarBackgroundColor = .mainGreen()
settings.style.selectedBarHeight = 4.0
settings.style.buttonBarMinimumLineSpacing = 0
settings.style.buttonBarItemsShouldFillAvailableWidth = true
settings.style.buttonBarLeftContentInset = 0
settings.style.buttonBarRightContentInset = 0
settings.style.selectedBarHeight = CGFloat(5)
super.viewDidLoad()
view.backgroundColor = .white
self.navigationItem.titleView = searchBar
}
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
businessSearchVC = BusinessSearchViewController(collectionViewLayout: UICollectionViewFlowLayout(), businesses: businesses)
couponSearchVC = CouponSearchViewController(collectionViewLayout: UICollectionViewFlowLayout(), coupons: coupons)
return [businessSearchVC!, couponSearchVC!]
}
fileprivate func search(query: String) {
//search request to API
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let searchTerms = searchBar.text else {
searchBar.endEditing(true)
return
}
searchBar.endEditing(true)
search(query: searchTerms)
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
//if empty (no text) then display view with search categories
}
}
这将导致如下视图:
一旦选择了类别,该类别视图就会消失,并显示我已经在商家和优惠券中使用的视图。