ios - 自定义键盘工具栏
问题描述
我为键盘创建了一个工具栏,但我不知道为什么我的工具栏位于当前视图的顶部。我需要将工具栏放在键盘顶部 这是现在的样子
这是代码
import Foundation
import SwiftUI
struct KeyboardObserving: ViewModifier {
@State var keyboardHeight: CGFloat = 0
@State var keyboardAnimationDuration: Double = 0
@State var currentHeight: CGFloat = 0
func body(content: Content) -> some View {
content
.padding([.bottom], keyboardHeight)
.edgesIgnoringSafeArea((keyboardHeight > 0) ? [.bottom] : [])
.animation(.easeOut(duration: keyboardAnimationDuration))
.onReceive(
NotificationCenter.default.publisher(for: UIResponder.keyboardWillChangeFrameNotification)
.receive(on: RunLoop.main),
perform: updateKeyboardHeight
)
}
func updateKeyboardHeight(_ notification: Notification) {
guard let info = notification.userInfo else { return }
// Get the duration of the keyboard animation
keyboardAnimationDuration = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double) ?? 0.25
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
currentHeight = keyboardSize.height
print("currentheiht \(currentHeight)") // output 336.0
}
guard let keyboardFrame = info[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
// If the top of the frame is at the bottom of the screen, set the height to 0.
if keyboardFrame.origin.y == UIScreen.main.bounds.height {
keyboardHeight = 0
} else {
// IMPORTANT: This height will _include_ the SafeAreaInset height.
keyboardHeight = keyboardFrame.height
print("keyboad \(keyboardHeight)")// output 336.0
}
}
}
extension View {
func keyboardObserving() -> some View {
self.modifier(KeyboardObserving())
}
}
解决方案
推荐阅读
- wordpress - Wordpress ContactForm7 不向电子邮件发送消息
- c++ - 使用 cmake 编译 iOS 动态库(.framework)
- jenkins - 如何在 Jenkins 上创建多个性能测试结果报告?
- java - 使用 Jersey 和 Moxy 编组 java POJO
- unix - pthread_cond_timedwait() 在 FreeBSD 上不起作用,返回 EPERM
- r - dplyr R中汇总函数中的ifelse()嵌套语句
- react-native - 可能未处理的承诺拒绝/错误:请求失败,状态码为 400
- r - R stringr按照它们出现的顺序提取关键字
- c# - 如何在基于桌面的应用程序中部署 SQL Server 更改
- java - 运营商?和 : 带有布尔值和 void