【问题标题】:Keypad and alert view don't disappear when tapping outside in Swift 4?在 Swift 4 中点击外部时,键盘和警报视图不会消失?
【发布时间】:2017-07-28 08:52:52
【问题描述】:

我是 swift 新手。我正在创建一个应用程序,当用户访问该应用程序时,它会显示评级应用程序警报框,然后当用户想要输入数字时。同时,键盘和警告框出现在视图中,无法从屏幕上消失。请帮我解决这个问题,当 2 个对话框出现时,让用户消失以响应键盘或评级警报。下面是我的代码。谢谢你的帮助。

这是导致不关闭的图像: http://imgur.com/a/7Hs9y

主视图控制器

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

AppDelegate 用于我的评级应用提醒。

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        let shortestTime: UInt32 = 1
        let longestTime: UInt32 = 10
        guard let timeInterval = TimeInterval(exactly: arc4random_uniform(longestTime - shortestTime) + shortestTime) else { return true }

        Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(AppDelegate.requestReview), userInfo: nil, repeats: false)

        return true


    }

@objc func requestReview() {
    if #available(iOS 10.3, *) {
        SKStoreReviewController.requestReview()
    } else {
        // Fallback on earlier versions
    }
}

【问题讨论】:

  • 开始尝试移除触摸覆盖
  • 它会自动要求您插入覆盖。 Overriding declaration requires an 'override' keyword
  • 我的意思是完全删除触摸开始调用而不仅仅是覆盖部分

标签: ios swift


【解决方案1】:

在 Swift 3 中,在您的项目中尝试我的代码并根据需要进行编辑

    override func viewDidLoad() {
            super.viewDidLoad()

    // *** Hide keyboard when tapping outside ***
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGestureHandler))
            view.addGestureRecognizer(tapGesture)
        }

    func tapGestureHandler() {
        view.endEditing(true)
    }

希望对你有用,

【讨论】:

  • 我尝试了你的方法,但由于某种原因,它不起作用。查看我更新的问题以查看我发布的图片。
【解决方案2】:

您可以使用 UITapGestureRecognizer 来隐藏您想要的内容,而不是“touchesBegan”

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多