TNKeyboardControlWindow 是一个小型实用工具,可以帮助您整合键盘平移消失功能。
下载文件夹 'TNKeyboardControlWindow' 并将其文件添加到您的项目中。
为了在整个应用程序中处理键盘平移,您需要在 UIWindow 级别处理。TNKeyboardControlWindow 为您做到了这一点。您需要在 AppDelegate 中导入 <TNKeyboardControlWindow.h>
并添加以下功能到 AppDelegate:
-(UIWindow *)window
{
return [TNKeyboardControlWindow window];
}
var window: UIWindow? {
get {
return TNKeyboardControlWindow.window as UIWindow?
} set {}
}
这将通过拖动自动在整个应用程序中处理键盘的消失!
如果您有自定义的 UIWindow 类并且仍然需要实现此功能,您可以简单地从 UIWindow 继承 TNKeyboardControlWindow 而不是 UIWindow。
在您的应用程序中的大多数 View Contorllers 中,您需要更新 UI 布局以适应屏幕上显示的键盘。为了做到这一点,请在 View Controller 中实现 <TNKeyboardListenerProtocol>
并添加该方法:
-(void)keyboardDidChangeFrame:(CGRect)frame {
}
func keyboardDidChangeFrame(_ frame: CGRect) {
bottomConstraint.constant = 8 + UIScreen.main.bounds.size.height - frame.origin.y
}
您还需要通过调用将您的 View Controller 添加为窗口的监听器:
[[TNKeyboardControlWindow window] addKeyboardFrameListener:self];
TNKeyboardControlWindow.window.addKeyboardFrameListener(self)
这最好在
viewDidAppear
中进行。
当您的 View Controller 不再显示时,您还需要通过调用将其移除:
[[TNKeyboardControlWindow window] removeKeyboardFrameListener:self];
TNKeyboardControlWindow.window.removeKeyboardFrameListener(self)
监听器协议有4个可选方法可以被实现,以避免使用NSNotificationCenter来监控键盘活动。
-(void)keyboardWillStartShowing;
-(void)keyboardDidFinishShowing;
-(void)keyboardWillStartHiding;
-(void)keyboardDidFinishHiding;
public func keyboardWillStartShowing()
public func keyboardDidFinishShowing()
public func keyboardWillStartHiding()
public func keyboardDidFinishHiding()
这就完成了!
项目中包含一个示例项目,其中包含不同的ViewControllers和不同的UI组件,它们会随着键盘的更新而更新。
您可以随心所欲地使用'TNKeyboardControlWindow'。不需要署名,但非常感谢。