![](/img/trans.png)
[英]UISearchController: searchBar and scopeBar overlap on first touch event
[英]How to add a searchBar with scopebar inside of a NavigationBar where the navBar automatically increase its height and shows the scopeBar
我閱讀了關於stackoverflow的所有問題,但沒有一個可以解決我的問題。 我創建了一個UICollectionView並在不使用情節提要的情況下將searchBar錨定在navigationBar內部!
class UserSearchController: UICollectionViewController ,.. {..
lazy var searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Enter username"
sb.barTintColor = UIColor.gray
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230)
sb.delegate = self
sb.showsCancelButton = true
return sb
}() ....
// adding the searchBar to the collectionView as subView and
anchoring it to the navigationBar
搜索欄顯示在navigationBar的內部,一切正常。 當我嘗試將范圍欄添加到搜索欄時,會出現問題。 我在搜索欄中添加了以下屬性
sb.showsScopeBar = true
sb.scopeButtonTitles = ["Username", "Hashtag"]
scopeBar隱藏在navigationBar后面,並且navBar不會自動增加其高度。 您只會看到灰色的背景。
將我的代碼添加到簡單的UiViewController中,一切正常
這段代碼在普通的VIewController內部起作用,在其中將searchBar作為subView添加並固定到視圖。
lazy var searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Enter username"
sb.barTintColor = UIColor.gray
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230)
sb.delegate = self
sb.scopeButtonTitles = ["Username", "Hashtag"]
sb.tintColor = UIColor.black
sb.barTintColor = UIColor.lightGray
return sb
}()
public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.showsScopeBar = true
searchBar.sizeToFit()
searchBar.setShowsCancelButton(true, animated: true)
return true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsScopeBar = false
searchBar.endEditing(true)
}
我的searchBar如下所示。 如果您開始輸入,則將顯示scopeBar,如果單擊“取消”,它將消失。 我想要相同的東西,但只在navBar內部:
如何在沒有Storyboard的navigationBar內添加帶有scopeBar的searchBar。 沒有任何簡單的方法。 請您參考我的代碼並使用我的代碼向我展示它的工作原理。 我還嘗試增加了NavigationBar的大小,但是它應該自動運行,我不知道這是否是正確的方法。
我認為這是您要尋找的一種實現:
您的細分控制器
// ( Declare Globaly )
var yourSegmentController = UISegmentedControl()
yourSegmentController.addTarget(self, action: #selector(ParentClass.filterChanged(_:)), for: .touchUpInside)
然后,你必須注冊您的自定義UICollectionViewCell
與你的類UICollectionView
。 在您的viewDidLoad
或在其中具有UICollectionView
的模塊中使用的任何初始化方法中執行此操作:
self.yourCollectionView.register(YourCustomUserCellClassHere.self, forCellWithReuseIdentifier: NSStringFromClass(YourCustomUserCellClassHere))
self.yourCollectionView.register(YourCustomHashtagCellClassHere.self, forCellWithReuseIdentifier: NSStringFromClass(YourCustomHashtagCellClassHere))
最后,在您的cellForItemAt
委托方法中,執行以下操作以查看選擇了哪個過濾器,並相應地返回單元格:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cellToReturn = UICollectionViewCell()
// USERS
if yourSegmentController.selectedSegmentIndex == 0 {
let customUserCell = self.yourCollectionView.dequeueReusableCell( withReuseIdentifier: NSStringFromClass(YourCustomUserCellClassHere), for: indexPath) as? YourCustomUserCellClassHere
// Do what you want with your custom cell here...
cellToReturn = customUserCell
}
// HASHTAGS
else if yourSegmentController.selectedSegmentIndex == 1 {
let customHashtagCell = self.yourCollectionView.dequeueReusableCell( withReuseIdentifier: NSStringFromClass(YourCustomHashtagCellClassHere), for: indexPath) as? YourCustomHashtagCellClassHere
// Do what you want with your custom cell here...
cellToReturn = customHashtagCell
}
return cellToReturn
}
這將檢查以查看選擇了哪個過濾器,然后返回適合該過濾器的單元格。
還要記住,每次用戶更改細分過濾器時,您都必須刷新UICollectionView
。
像這樣刷新它:
// Put this method at the same level where your cellForItemAt function is but not inside
func filterChanged (_ sender:UISegmentedControl) {
self.yourCollectionView.reloadData()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.