I have a scene that is very similar to the iOS Maps app - some content and a table view with a search bar at the top that you can pull up over the content. What I want is for the user to be able to pan starting on the search bar, regardless of whether it is active or not.
On its own, this work, as long as the pan does not start inside the text field of the search bar. To overcome this, I put a dummy UIView
mask with a gesture recogniser on top of the search bar, similar to this. However, as soon as I tap and, thus, the searchBar becomes first responder, the mask goes to the background and does not accept touches (note that I do not set isHidden
to false
on the mask). The only way to go back to panning is to searchBarContainerView.bringSubview(toFront: theMask)
. The problem is that unless the searchBar has resigned first responder, this has no effect. I tried subclassing a UIView for the container and changed layoutSubviews
to:
super.layoutSubviews()
if let searchBarMask = self.searchBarMask {
bringSubview(toFront: searchBarMask)
}
but without effect. Any suggestions? I am willing to depart from the UIView-mask pattern if necessary.
The hierarchy is something like:
UIView
- ScrollView (the content)
- ContainerView (the pull up thingy)
UIView (from the Container View above, in a separate UIViewController, name it XController)
- UIView - search placeholder
- UITableView
The search bar is added in the XController as:
override func viewDidLoad() {
self.controller = UISearchController(nil)
self.searchPlaceholder.addSubview(self.controller.searchBar)
...
self.mask = UIView(frame: self.controller.searchBar.frame)
self.searchPlaceholder.insertSubview(self.mask, aboveSubview: self.controller.searchBar)
}