Custom keyboard extension left edge detecting touch after a second.

I'm creating a custom keyboard extension. So as a result, there are buttons which are pinned to the left edge of the keyboard. (Think of q key as an example).

The logic of the key presses go something like this:
  • Button detects a touchDown event and shows the magnified text which you normally see in system keyboard when tapping a key.

  • Button detects a touchUpInside/touchDragOutside event and the magnified text disappears, again very similar to the system keyboard.

This logic worked for all the buttons which were not pinned to the left edge of the keyboard. But for the buttons that were pinned to the left edge, the touchDown events were being detected after a second. So you can see this is obviously bad because I want to see the magnified text right after I place my finger on the button.

WHAT I TRIED AS AN ALTERNATIVE:
I removed all the touchDown, touchUpInside and touchDragOutside events from the button and disabled all their user interaction. Then I implemented to touches functions(touchesBegan, touchesEnded, etc.) and observed the touch locations on the background view. Surprisingly, even in this case, the touchesBegan function was called after a second after I placed my finger on the left edge of the screen and as usual, the touchesBegan function called just fine in the rest of the screen.

Here's the code for the touches function:
Code Block
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = event?.allTouches?.first else { return }
let location = touch.location(in: self.touchView)
print(location)
}


What exactly is happening here? And what can I do to avoid this problem?

NOTE: It works fine in simulator for some reason but has a problem with real devices.

Hi, I faced this exact same problem while building my custom keyboard extension and after extensive research I finally found a complete solution that fixes it permanently. The root cause is that iOS system gestures own the screen edges by default and intercept your touches before they reach your keyboard. Three things must be fixed simultaneously — fixing only one or two will not work completely. the solution in here:

https://gist.github.com/hamdiqbal/f642e2d73d58e76f65a76dc10b44e725

Why this works:

preferredScreenEdgesDeferringSystemGestures tells iOS your keyboard owns the left and right edges Calling setNeedsUpdateOfScreenEdgesDeferringSystemGestures() in BOTH viewDidLoad and viewWillAppear is critical — one call alone is not enough because iOS resets edge ownership during transitions

Walking the full view hierarchy catches system gesture recognizers attached to parent views outside your control

Disabling all three delay properties (delaysTouchesBegan, delaysTouchesEnded, cancelsTouchesInView) removes every source of delay

Specifically targeting and disabling UIScreenEdgePanGestureRecognizer removes the exact system gesture stealing your edge touches

I tested this on iOS 16, 17 and 18 across iPhone 12, 13, 14 and 15 series. All edge keys now respond instantly with zero delay. Hope this helps everyone who has been struggling with this!

Custom keyboard extension left edge detecting touch after a second.
 
 
Q