我有一个带有搜索栏和tableview的模态视图控制器,基本上是一个弹出式搜索框,使用pop-over segue呈现.在顶部有一个带取消按钮的UISearchBar.我正在尝试使用该搜索栏上的取消按钮关闭视图控制器.
我尝试了很多方法......
-(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
和
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
和委托方法
[self.delegate dismissModalViewController:self]
Run Code Online (Sandbox Code Playgroud)
同
-(void) dismissModalViewController:(UIViewController*) viewToDismiss
{
[viewToDismiss dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
我不知道UISearchBar是否在干扰,但这似乎是一个合理的假设.否则这是一个常见的话题,我为提出一个之前可能已经回答的问题而道歉但是我已经阅读了fm并用谷歌搜索直到我是蓝色的并且仍然没有结果.
我有一个UITableViewController班级,我正在实施一个UISearchController.我添加了以下代表:
class EmployeesTableView: UITableViewController, NSFetchedResultsControllerDelegate,UISearchResultsUpdating{
Run Code Online (Sandbox Code Playgroud)
我正在进口两个UIKit和CoreData.我收到以下错误:
"Type 'CustomTableViewController' does not conform to protocol UISearchResultsUpdating"
Run Code Online (Sandbox Code Playgroud)
我需要做些什么才能使控制器符合协议?
我们如何更改搜索控制器中取消按钮的标题?

它怎么UISearchController做呢?
UISearchBar为类似导航栏的视图?UISearchBar放入另一个视图层次结构并在以后恢复?UIViewController表示API 完成所有这些的?我可以在UISearchController不使用私有API 的情况下自行创建吗?
我在界面构建器中添加了一个搜索栏和搜索显示控制器到我的应用程序.我无法正确地进行deinit(dealloc).
它显示以下行为(swift2,ios9):
如果我在导航控制器中选择"返回"而不是选择项目,则会出现相同的行为.
code removed - refer to COMPLETE CODE at bottom of post.
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏!
更新进一步测试表明,从视图控制器中删除progressHud/loadingHud完全没有影响DEINIT没有被调用.它必须与tableview或searchcontroller本身有关...
更新2我尝试在viewWillDissapear中调用searchBarCancelButtonClicked()方法,但仍然没有释放.即使你点击"取消"然后导航它就会......
更新3将willDisappear/didDisappear更改为以下对DEINIT没有影响 - 但不会给出错误的界面问题(感谢Polina).我试图找出我能发布的任何东西,但到目前为止还没有运气.
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
searchBarCancelButtonClicked(searchController.searchBar)
}
override func viewDidDisappear(animated: Bool) {
print("View did disappear")
searchController.searchBar.resignFirstResponder()
searchController.searchBar.endEditing(true)
searchController.active = false
loadingHud.removeFromSuperview()
progressHud.removeFromSuperview()
searchController.searchBar.delegate = nil
searchController.searchResultsUpdater = nil
searchController = nil
tableView = nil
super.viewDidDisappear(true)
}
Run Code Online (Sandbox Code Playgroud)
更新4我找不到答案.真的希望有人可以帮忙!
更新5响应@ConfusedByCode - 我更新了以下方法,以便[unowned self] in在所有闭包或后台线程操作中使用:
code removed - …Run Code Online (Sandbox Code Playgroud) 我的tableview标题中有一个搜索栏.为此,我正在使用uisearchcontroler.但是当我在搜索栏中发短信时它会更新tablview数据,我需要在点击键盘上的搜索按钮时更新tablview,因为我在api中获取搜索数据,每次当我在搜索栏中发短信时它都会请求并且需要很长时间.我怎么解决这个问题?
var resultSearchController = UISearchController()
var indicator:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
var ref=UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubviewToFront(view)
indicator.startAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
getRequests.getType1(tableView,spinner: indicator)
ref.addTarget(self, action: #selector(Catalog1TableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
ref.attributedTitle = NSAttributedString(string: "????????")
tableView.addSubview(ref)
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
indicator.startAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
tableView.reloadData()
getRequests.getProduct(tableView, spinner: indicator,name: searchController.searchBar.text!)
for …Run Code Online (Sandbox Code Playgroud) 我遇到了很多的问题与iOS 11和显示UISearchController通过提出过来导航栏(如描述这里,例如从苹果教程)
@IBAction func searchAction(sender: UIBarButtonItem) {
// Create the search controller and specify that it should present its results in this same view
searchController = UISearchController(searchResultsController: nil)
// Make this class the delegate and present the search
self.searchController.searchBar.delegate = self
presentViewController(searchController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
它正在隐藏应用程序UINavigationBar并显示UISearchController搜索栏.
问题1.在iOS 11上,它导致搜索栏第一次出现时与状态重叠(再次尝试后不会重叠).
UISearchController第一次呈现.状态栏和搜索栏之间没有空格.
UISearchController再次呈现,UINavigationBar更大,搜索栏是更低的状态栏.
问题2在iPhone X上,它不会覆盖整个空间
我花了好几个小时试图搞清楚.还有其他的,简单的方法是在点击例如后在iOS 11上显示搜索栏.导航栏中的搜索图标?有没有办法修复UISearchControlleriPhone X上的导航栏高度和空间?
有没有人成功地保留了UISearchBar在tableHeaderView一个UITableView在ios11?iPhone X在景观中通常会出现问题:
Apple建议使用新searchController属性:
if (@available(iOS 11.0, *)) {
self.navigationItem.searchController = self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
} else {
self.tableView.tableHeaderView = self.searchController.searchBar;
}
Run Code Online (Sandbox Code Playgroud)
然而,这并不总是有效.这是一个代码示例:
嵌入UISplitViewController时,UINavigationBar中缺少iOS11 UISearchBar
所以,试图保持searchBar在tableHeaderView-切实可行的第一步是应用适当的自动布局限制:
if (@available(iOS 11.0, *)) {
UILayoutGuide *guide = self.tableView.safeAreaLayoutGuide;
searchBar.translatesAutoresizingMaskIntoConstraints = NO;
[searchBar.leftAnchor constraintEqualToAnchor:guide.leftAnchor].active = YES;
[searchBar.rightAnchor constraintEqualToAnchor:guide.rightAnchor].active = YES;
}
Run Code Online (Sandbox Code Playgroud)
在searchBar显示时,那么正确的尺寸:
但是,UISearchBar激活时会出现问题:
这似乎是一个老问题.有许多stackoverflow问题和答案与这样的问题有关.在这种情况下,在检查视图层次结构时,似乎设置的宽度UISearchBar不正确.所以这可以纠正:
- (void)didPresentSearchController:(UISearchController *)searchController NS_AVAILABLE_IOS(11_0)
{
if (@available(iOS 11.0, *)) {
CGRect frame = …Run Code Online (Sandbox Code Playgroud) 我知道如何设置独立外观,UISearchBar如下所示。
let searchField = searchBar.value(forKey: "searchField") as? UITextField
if let field = searchField {
field.backgroundColor = UIColor.defaultBackgroundColor
field.layer.cornerRadius = 15.0
field.textColor = .white
field.tintColor = .white
field.font = UIFont.systemFont(ofSize: fl(13))
field.layer.masksToBounds = true
field.returnKeyType = .search
}
Run Code Online (Sandbox Code Playgroud)
但这不适用于UISearchController。
我想将占位符和左放大镜图标的文本颜色设置为纯白色。(似乎现在它们上面有一个彩色层)。
另外,输入文本现在是黑色的,我也希望它也是白色的。
最后,我要修改以下属性。
1. textField背景颜色
2. textFiled占位符文本颜色
3. textFiled文本颜色
4. textFiled字体
有人知道怎么做吗?
将以下内容与您的代码添加到viewDidAppear中:
let placeholderString = NSAttributedString(string: "Placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
field.attributedPlaceholder = placeholderString
let iconView = field.leftView as! UIImageView
iconView.image = iconView.image?.withRenderingMode(.alwaysTemplate)
iconView.tintColor = .white
Run Code Online (Sandbox Code Playgroud)
Updata:
放入这些设置 …
当我将项目更新到iOS 13时,我遇到了异常问题。我有显示和处理某些UISearchController动作的逻辑(下面的代码),所有部分在iOS 11和12中均正常运行。
我的任务是在导航栏中添加搜索按钮,以UISearchController在按钮操作后显示。
但是在iOS 13中,我遇到了2个问题:
从按钮动作调用的代码
func showSearch() {
let searchResultsController = LUSearchResultsViewController()...
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
searchController.searchResultsUpdater = searchResultsController
navigationItem.searchController = searchController
definesPresentationContext = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
searchController.isActive = true
}
}
Run Code Online (Sandbox Code Playgroud)
只有一种解决方案可以帮助我显示搜索控制器,而与以前的iOS版本没有任何区别。
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
searchController.isActive = true
}
Run Code Online (Sandbox Code Playgroud)
在iOS 13,iOS 11和12上运行的代码无需asyncAfter
帮助我隐藏UISearchController和设置iOS 11和12(而非13)中的导航栏初始状态的代码。在SearchBar上点击“ 取消 ” 后,UISearchController开始调用委托方法。
//MARK: UISearchControllerDelegate
public func willDismissSearchController(_ searchController: UISearchController) {
self.navigationItem.searchController = …Run Code Online (Sandbox Code Playgroud) ios ×9
swift ×5
uisearchbar ×4
uitableview ×3
ios11 ×2
ios8 ×2
cocoa-touch ×1
ios13 ×1
iphone ×1
retain ×1
swift5 ×1
uikit ×1
uitextfield ×1