[英]Cancel button is not shown in UISearchBar
我有UICollectionView
。 在點擊搜索按鈕UINavigationBar
,我加入UISearchController
的searchbar
作為titleview的用於UINavigationItem
。 對於iPhone它運作正常。 對於iPad, cancel
按鈕不會顯示。 僅搜索欄占據整個寬度。
任何人都可以幫我解決這個問題嗎? 提前致謝。
添加到導航欄時,iOS7不顯示取消按鈕。您可以將搜索欄放在另一個視圖中。
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds];
[viewForSearchBar addSubview:searchBar];
self.navigationItem.titleView = viewForSearchBar;
我有同樣的問題,在iPhone上搜索取消顯示良好,但在iPad上它沒有。
將UISearchBar
包裝在另一個UIView
的解決方法對我來說效果不好,因為它在旋轉時具有不同的外觀和錯誤的寬度。
我的解決方案很簡單 - 使用搜索WITHOUT取消,並添加取消作為UIBarButtonItem
。
試試這個。 為節目取消按鈕添加復選標記。
Added rightBarButtonItem with selector will work fine for me. And adding searchBar inside view before setting to navigation title view was not showing properly.
Code:-
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.dismissView))
func dismissView() {
if self.controller?.navigationController?.popViewController(animated: true) == nil {
self.controller?.dismiss(animated: true, completion: nil)
}
}
Swift版本: -
我嘗試了@Nikita Khandelwal方法,但它仍然不適合ipad視圖。 這是快速代碼,作為更正的答案給出: -
let searchBar: UISearchBar = UISearchBar()
searchBar.showCancelButton = true
searchBar.placeholder = "Search Your Job Title"
searchBar.fitToSize()
searchBar.delegate = self //do not need if you delegate searchBar
let viewForSearchBar: UIView = UIView(frame: searchBar.bounds)
viewForSearchBar.addSubview(searchBar)
self.navigationItem.titleView = viewForSearchBar
*********但還有另一種方法可以正確設置取消按鈕並適合視圖: -
將搜索欄設置為導航欄標題視圖: -
let searchBar: UISearchBar = UISearchBar() searchBar.showCancelButton = true searchBar.placeholder = "Search Your Job Title" searchBar.delegate = self //do not need if you delegate searchBar self.navigationItem.titleView = searchBar
將“條形圖”按鈕拖放到視圖控制器的右側,並將其命名為“取消”。
然后將該按鈕連接到此功能: -
@IBAction func iPadCancelButton(sender: AnyObject) { UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil) self.dismissViewControllerAnimated(true, completion: nil) }
根據蘋果文檔setShowsCancelButton
對於在iPad上運行的應用程序,即使為showsCancelButton參數指定YES,也不會顯示取消按鈕。
我不確定替代品,但這是蘋果為我們提供的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.