SwiftUI 点击空白处隐藏键盘
2020-08-25 本文已影响0人
大_瓶_子
第一种方法:
UIApplication.shared.windows.forEach { (window) in
if window.isKeyWindow {
window.endEditing(true)
}
}
第二种方法:
UIApplication.shared.windows.first?.endEditing(true)
在空白处点击,先生成底部背景使用ZStack包裹
ZStack{
Color.white
.frame(width: ScreenWidth, height: ScreenHeight, alignment: .center)
.onTapGesture {
UIApplication.shared.windows.forEach { (window) in
if window.isKeyWindow {
window.endEditing(true)
}
}
}
}
这里的Color相当于UIView
let window = SceneDelegate().window
let ScreenWidth = window?.bounds.size.width
let ScreenHeight = window?.bounds.size.height