How to debug who is eating my touches in UIKit?

前端 未结 7 1493
孤街浪徒
孤街浪徒 2021-01-30 17:16

I can\'t figure out why tapping on text fields and buttons in my view is not working. I\'ve checked all the obvious things like whether userInteractionEnabled is set to YES, whe

7条回答
  •  离开以前
    2021-01-30 17:43

    You can subclass UIWindow and override -[UIWindow sendEvent]:, then when it is called, use -[UIWindow hitTest:withEvent:] to test which view will receive the event.

    You can then call -[UIView recursiveDescription] to print some debug information help you understand why that view received the touch event.

    Remember to call [super sendEvent:] when you are done.


    For those who use Storyboard and want to know how can we change the main application window class:

    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow? = TestWindow()
        // ....
    }
    

    Just provide the default value for AppDelegate's window var. When the app is launched the UIApplicationMain will instantiate a delegate and ask it for the window. If the window is nil it will create a new one automatically. But if we provide a default value here, it will be used all over the app.

提交回复
热议问题