思路:
- 获取到键盘所在的window
- 自定义view添加到window上
获取window的代码如下
- (UIWindow *)keyboardWindow{
UIWindow *keyboardWindow = nil;
for(UIWindow *window in [UIApplication sharedApplication].windows)
{
if([window isKindOfClass:NSClassFromString(@"UIRemoteKeyboardWindow")])
{
keyboardWindow = window;
}
}
return keyboardWindow;
}
添加view代码如下
键盘出现:
- (void)keybordShowAnimalByKeyboardHeight:(float)keyboardHeight AndAnimalTime:(float)keyboardDuration{
UIWindow *keyboardWindow = [self keyboardWindow];
// 添加自定义view
if (![keyboardWindow.subviews containsObject:self.toolBarView]) {
[keyboardWindow addSubview:self.toolBarView];
[keyboardWindow addSubview:self.keyBoardInputView];
}
// 键盘工具栏
[self.keyBoardInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(keyboardWindow);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(keyboardHeight);
}];
// 键盘覆盖层
[self.toolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.keyBoardInputView.mas_top);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(TOOL_BAR_HEIGHT);
}];
// 更新约束
[keyboardWindow layoutIfNeeded];
}
键盘隐藏:
- (void)keybordHiddenAnimalByKeyboardHeight:(float)keyboardHeight AndAnimalTime:(float)keyboardDuration{
UIWindow *keyboardWindow = [self keyboardWindow];
if (![keyboardWindow.subviews containsObject:self.toolBarView]) {
[keyboardWindow addSubview:self.toolBarView];
[keyboardWindow addSubview:self.keyBoardInputView];
}
// 修改为以前的约束(距下边距0)这么写的目的是为了动画效果
[self.toolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(keyboardWindow.mas_bottom);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(TOOL_BAR_HEIGHT);
}];
[self.keyBoardInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.toolBarView.mas_bottom);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(keyboardHeight);
}];
// 更新约束
[keyboardWindow layoutIfNeeded];
}
网友评论