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
UIView (from the Container View above, in a separate UIViewController, name it XController)
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)
}
I ended up doing something similar, which is as follows. I put a UIView as a mask, but this time it was directly in the search bar view:
self.searchBar.addSubview(theMask)
self.searchBar.bringSubview(toFront: theMask)
// add pan gesture recogniser on theMask
I don't have any hide/unhide logic and it works great. There are some issues, for example you cannot pan starting on the Cancel button and tapping on the search field is totally ignored, but I think this has most to do with correctly configuring gesture recognising.