在 Swift 4 中敲击外部时,键盘和警报视图不会消失吗?



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

这是导致不关闭的图像: https://i.stack.imgur.com/JSgHS.jpg

主视图控制器

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

我的评级应用警报的应用委托。

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
}
}

在 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)
}

我希望这对你有用,

您可以使用UITapGestureRecognizer隐藏所需的内容,而不是"touchesBegan">

最新更新