IOS swift 如何让我的子视图显示在我的 SearchBar 下方
IOS swift How can I get my SubView to appear below my SearchBar
我有一个嵌入在表单中的 UISearchBar 所以我想做的是一旦有人点击 SearchBar 我想要一个 TableView 作为 subView 弹出。我可以正常工作,但是我希望 SubView 出现在 SearchBar 下,以便用户可以继续搜索,我的 SubView 接管了整个页面。以下是我试图使功能正常工作的方式:Trulia App。
现在,如果您单击 SearchBar,请注意带有相应视图的 searchBar,子视图会出现在 SearchBar 的正下方。如前所述,对我而言,一切正常,除了 subView 接管了整个页面并且没有出现在 SearchBar 下方。这是我的代码,下面的第三张图片是我的故事板,这是我的代码
class RegistrationViewController: UIViewController,UISearchBarDelegate {
@IBOutlet weak var Location: UISearchBar!
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.setShowsCancelButton(false, animated: true)
let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "zip_searches") as! ExampleTableViewController
self.addChildViewController(Popup)
Popup.view.frame = self.view.frame
self.view.addSubview(Popup.view)
Popup.didMove(toParentViewController: self)
return true
}
}
[
iOS 为此有 UISearchController。下面是将搜索栏添加和限制到故事板中现有容器视图以及设置 tableViewController 以在用户开始输入时显示的代码。
locationSearchResultTableViewController = storyboard?.instantiateViewController(withIdentifier: String(describing: LocationSearchResultTableViewController.self)) as! LocationSearchResultTableViewController
locationSearchResultTableViewController.delegate = self
searchController = UISearchController(searchResultsController: locationSearchResultTableViewController)
searchController.searchResultsUpdater = locationSearchResultTableViewController
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
searchContainer.addSubview(searchController.searchBar)
searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
let attributes: [NSLayoutAttribute] = [.top, .bottom, .left, .right]
attributes.forEach { NSLayoutConstraint(item: self.searchController.searchBar, attribute: [=10=], relatedBy: .equal, toItem: searchController.searchBar.superview, attribute: [=10=], multiplier: 1, constant: 0)}
我找到了解决问题的简单方法。您只需在顶部有一个 SearchBar,然后创建第二个 UIView 并将其放在 SearchBar 下方。然后你连接那个新的 UIView 比如
@IBOutlet weak var view2: UIView!
那么当涉及到框架部分时,你只需这样做
Popup.view.frame = self.view2.frame
就是这样,当您单击 SearchBar 时,View2 将充当弹出窗口并覆盖 searchBar 下方的所有内容。
我有一个嵌入在表单中的 UISearchBar 所以我想做的是一旦有人点击 SearchBar 我想要一个 TableView 作为 subView 弹出。我可以正常工作,但是我希望 SubView 出现在 SearchBar 下,以便用户可以继续搜索,我的 SubView 接管了整个页面。以下是我试图使功能正常工作的方式:Trulia App。 现在,如果您单击 SearchBar,请注意带有相应视图的 searchBar,子视图会出现在 SearchBar 的正下方。如前所述,对我而言,一切正常,除了 subView 接管了整个页面并且没有出现在 SearchBar 下方。这是我的代码,下面的第三张图片是我的故事板,这是我的代码
class RegistrationViewController: UIViewController,UISearchBarDelegate {
@IBOutlet weak var Location: UISearchBar!
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.setShowsCancelButton(false, animated: true)
let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "zip_searches") as! ExampleTableViewController
self.addChildViewController(Popup)
Popup.view.frame = self.view.frame
self.view.addSubview(Popup.view)
Popup.didMove(toParentViewController: self)
return true
}
}
[
iOS 为此有 UISearchController。下面是将搜索栏添加和限制到故事板中现有容器视图以及设置 tableViewController 以在用户开始输入时显示的代码。
locationSearchResultTableViewController = storyboard?.instantiateViewController(withIdentifier: String(describing: LocationSearchResultTableViewController.self)) as! LocationSearchResultTableViewController
locationSearchResultTableViewController.delegate = self
searchController = UISearchController(searchResultsController: locationSearchResultTableViewController)
searchController.searchResultsUpdater = locationSearchResultTableViewController
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
searchContainer.addSubview(searchController.searchBar)
searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
let attributes: [NSLayoutAttribute] = [.top, .bottom, .left, .right]
attributes.forEach { NSLayoutConstraint(item: self.searchController.searchBar, attribute: [=10=], relatedBy: .equal, toItem: searchController.searchBar.superview, attribute: [=10=], multiplier: 1, constant: 0)}
我找到了解决问题的简单方法。您只需在顶部有一个 SearchBar,然后创建第二个 UIView 并将其放在 SearchBar 下方。然后你连接那个新的 UIView 比如
@IBOutlet weak var view2: UIView!
那么当涉及到框架部分时,你只需这样做
Popup.view.frame = self.view2.frame
就是这样,当您单击 SearchBar 时,View2 将充当弹出窗口并覆盖 searchBar 下方的所有内容。