I'm trying to override the "swipe down from the top of the screen" gesture (the same gesture that opens the Notification Center) in my iOS app.
From my understanding, the correct way to do so is to override the preferredScreenEdgesDeferringSystemGestures()
method on the view controller and use the UIScreenEdgePanGestureRecognizer
to handle the gesture.
In my case, the default system gesture is successfully blocked (Notification Center is not opened, only the gray indicator appears at the top of the screen), but the gesture recognizer is not triggered. Here is the code for the view controller:
class ViewController: UIViewController {
override func preferredScreenEdgesDeferringSystemGestures() -> UIRectEdge {
return .top
}
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(swipeFromTop))
gestureRecognizer.edges = .top
view.addGestureRecognizer(gestureRecognizer)
}
@objc func swipeFromTop() {
print("swipe from top") // not printed
}
}
I tried the same thing for the "swipe up from the bottom of the screen" gesture and that works as expected (Control Center is not opened and the gesture recognizer is triggered).
I'm testing on iPhone 7 running iOS 11.4.
gestureRecognizer
to debug what's going on - e.g. if it receives touches, if it "begins" and if it fails. – Eugene DudnykViewController
inside of navigation controller? – Eugene DudnykUIViewController
in aUIWindow
(the default setup you get with "Single View App") – denjizswipeFromTop
is being called, if I start to swipe a bit below the status bar. – Eugene Dudnyk