TNKeyboardControlWindow 1.0.7

TNKeyboardControlWindow 1.0.7

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2017年5月

Tawa Nicolas维护。



总结

TNKeyboardControlWindow 是一个小型实用工具,可以帮助您整合键盘平移消失功能。

安装

手动安装

下载文件夹 'TNKeyboardControlWindow' 并将其文件添加到您的项目中。

集成

为了在整个应用程序中处理键盘平移,您需要在 UIWindow 级别处理。TNKeyboardControlWindow 为您做到了这一点。您需要在 AppDelegate 中导入 <TNKeyboardControlWindow.h> 并添加以下功能到 AppDelegate:

Objective-C

-(UIWindow *)window
{
	return [TNKeyboardControlWindow window];
}

Swift

var window: UIWindow? {
	get {
		return TNKeyboardControlWindow.window as UIWindow?
	} set {}
}

这将通过拖动自动在整个应用程序中处理键盘的消失!

如果您有自定义的 UIWindow 类并且仍然需要实现此功能,您可以简单地从 UIWindow 继承 TNKeyboardControlWindow 而不是 UIWindow。

处理 UI

在您的应用程序中的大多数 View Contorllers 中,您需要更新 UI 布局以适应屏幕上显示的键盘。为了做到这一点,请在 View Controller 中实现 <TNKeyboardListenerProtocol> 并添加该方法:

Objective-C

-(void)keyboardDidChangeFrame:(CGRect)frame {
}

Swift

func keyboardDidChangeFrame(_ frame: CGRect) {
	bottomConstraint.constant = 8 + UIScreen.main.bounds.size.height - frame.origin.y
}

您还需要通过调用将您的 View Controller 添加为窗口的监听器:

Objective-C

[[TNKeyboardControlWindow window] addKeyboardFrameListener:self];

Swift

TNKeyboardControlWindow.window.addKeyboardFrameListener(self)

这最好在 viewDidAppear 中进行。

当您的 View Controller 不再显示时,您还需要通过调用将其移除:

Objective-C

[[TNKeyboardControlWindow window] removeKeyboardFrameListener:self];

Swift

TNKeyboardControlWindow.window.removeKeyboardFrameListener(self)

可选监听器方法

监听器协议有4个可选方法可以被实现,以避免使用NSNotificationCenter来监控键盘活动。

Objective-C

-(void)keyboardWillStartShowing;
-(void)keyboardDidFinishShowing;

-(void)keyboardWillStartHiding;
-(void)keyboardDidFinishHiding;

Swift

public func keyboardWillStartShowing()
public func keyboardDidFinishShowing()

public func keyboardWillStartHiding()
public func keyboardDidFinishHiding()

这就完成了!

项目中包含一个示例项目,其中包含不同的ViewControllers和不同的UI组件,它们会随着键盘的更新而更新。

备注

您可以随心所欲地使用'TNKeyboardControlWindow'。不需要署名,但非常感谢。