取消button不显示在UISearchBar中
我有UICollectionView
。 单击UINavigationBar
searchbutton,我将UISearchController
的search栏添加为UINavigationItem
titleview。 对于iPhone它正在正常工作。 对于iPad cancel
button不显示。 单独的search栏占据整个宽度。
任何人都可以帮我解决这个问题吗? 提前致谢。
添加到导航栏时,iOS7不显示取消button。您可以将search栏放在另一个视图中。
UISearchBar *searchBar = [UISearchBar new]; searchBar.showsCancelButton = YES; [searchBar sizeToFit]; UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds]; [viewForSearchBar addSubview:searchBar]; self.navigationItem.titleView = viewForSearchBar;
我有同样的问题,在iPhone上search取消显示,但在iPad上没有。
将UISearchBar
包装到另一个UIView
的解决方法对我来说不是很好,因为它具有不同的外观和错误的旋转宽度。
我的解决scheme是一个简单的使用search没有取消,并添加取消作为UIBarButtonItem
。
尝试这个。 添加显示取消button的复选标记。
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
*********但有另一种方法正确设置取消button,适合视图: –
-
将search栏设置为导航栏标题视图: –
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
-
将Barbutton拖放到视图控制器的右侧,并将其命名为Cancel。
-
然后将该button连接到此function: –
@IBAction func iPadCancelButton(sender: AnyObject) { UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil) self.dismissViewControllerAnimated(true, completion: nil) }
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) } }
- Xcode版本6.1(6A1030) – Apple Mach O-Linker错误 – 构建
- UITableViewController不加载数据
- 问题与NSSearchPathForDirectoriesInDomains和持久性数据
- AppDelegate文件在哪里适合MVC?
- 如何使用NSCoding作为结构的c数组? (MKPolyline)
- 一个错误? NSDateFormatter dateFromString仅为此date返回null
- iOS:如何从文档目录中删除具有特定扩展名的所有现有文件?
- iOS本地化,模拟器和设备总是使用Base.lproj而不是用户首选的区域设置
- 以编程方式为iPhone创建x509证书而不使用OpenSSL