SwiftUI 关闭键盘

需要用到Uikit的代码:然后在任意View里面就可以调用hideKeyBoard了。

import UIKit

#if canImport(UIKit)
extension View {
    func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}
#endif

你可能感兴趣的:(SwiftUI 关闭键盘)