![](/img/trans.png)
[英]Cancel Button in UISearchController Causes SearchBar Glitch
[英]Searchbar cancel button not visible only on iPad
首先我想說我沒有在Swift
找到任何好的答案。 我創建了search bar
。 在按鈕單擊它顯示和取消隱藏searchbar
。 它工作正常,取消按鈕在所有iPhones
上都可見,但由於某種原因在iPad
不可見。 應該是什么原因造成的?
這就是我創建搜索searchbar
:
//Create searchbar
func createSearchBar(){
searchBar.showsCancelButton = true
searchBar.tintColor = UIColor(red:0.184, green:0.996, blue:0.855, alpha:1.00)
searchBar.placeholder = "Search brands"
searchBar.delegate = self
searchBar.hidden = false
searchBar.alpha = 0
navigationItem.titleView = searchBar
navigationItem.setLeftBarButtonItem(menuButton, animated: true)
navigationItem.setRightBarButtonItem(searchButtton, animated: true)
UIView.animateWithDuration(0.5, animations: {
self.searchBar.alpha = 1
}, completion: { finished in
self.searchBar.becomeFirstResponder()
})
}
面對同樣的一個我的項目。 我在蘋果論壇上發過貼,很多開發人員評論說是 xcode 錯誤。 所以我為 ipad 視圖手動添加了取消按鈕
func configureCancelBarButton() {
let cancelButton = UIBarButtonItem()
cancelButton.image = UIImage(named: "cancelButton")
cancelButton.action = #selector(viewController.ipadCancelButton(_:))
cancelButton.target = self
self.navigationItem.setRightBarButtonItem(cancelButton, animated: true)
}
我之前發布了一個關於這個問題的答案。 也檢查一下。 :)
https://developer.apple.com/documentation/uikit/uisearchbar/1624283-showscancelbutton
對於在 iPad 上運行的應用程序,此屬性的值將被忽略,並且不顯示取消按鈕。
蘋果在文檔中添加了它!
如果您不使用 UISearchbar 作為 NavigationBarItem,則 UISearchbar 的取消按鈕將在 iPad 中工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.