首页 > 解决方案 > 如何检测状态栏 iOS 13 上的触摸

问题描述

我在状态栏框架的 AppDelegate keyWindow 中添加了一个自定义视图。在 iOS 13 之前,touchbegan被调用,使用它来比较触摸位置和状态栏坐标。

我进行了很多搜索,但找不到任何为什么 touchbegain 停止被调用的原因。

我还尝试通过在 iOS 13 中隐藏状态栏而不是添加状态栏框架的子视图,这样 touchbegan 被调用,但是当我隐藏状态栏时视图正在上升。

如果有人对此有任何解决方案,请发布您的回复。

标签: iosobjective-cswift

解决方案


尝试在 AppDelegate.swift 中使用此代码

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)

    let statusBarRect = UIApplication.shared.statusBarFrame
    guard let touchPoint = event?.allTouches?.first?.location(in: self.window) else { return }

    if statusBarRect.contains(touchPoint) {
      // tap on statusbar, do something
    }
}

推荐阅读