需求:给webview添加自定义键盘,样式仿qq聊天页面的键盘
思路:
监听键盘show和hidden的通知
获取键盘所在的window
将自定义view添加在键盘所在的window,并将其根据键盘位置放在指定位置。
实现代码如下:(代码不去,因为与工程中耦合太高。。仅代表思想)
- (instancetype)init{
self = [super init];
if (self) {
[self addNoti];
}
return self;
}
- (void)dealloc{
[self removeNoti];
}
#pragma mark 通知
- (void)addNoti{
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)removeNoti{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
// 获取键盘基本信息(动画时长与键盘高度)
NSDictionary *userInfo = [aNotification userInfo];
CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 添加view 并设置约束
[self keybordShowAnimalByKeyboardHeight:keyboardHeight AndAnimalTime:keyboardDuration];
// 回调
if (self.keybordShowBlock) self.keybordShowBlock(keyboardHeight,YES);
}
- (void)keyboardWillHide:(NSNotification *)aNotification{
// 获得键盘动画时长
NSDictionary *userInfo = [aNotification userInfo];
CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 设置约束
[self keybordHiddenAnimalByKeyboardHeight:keyboardHeight AndAnimalTime:keyboardDuration];
// 回调
if (self.keybordShowBlock) self.keybordShowBlock(keyboardHeight,YES);
// 设置键盘
self.isKeyBordView = YES;
}
// 获取键盘所在的window
- (UIWindow *)keyboardWindow{
UIWindow *keyboardWindow = nil;
for(UIWindow *window in [UIApplication sharedApplication].windows)
{
if([window isKindOfClass:NSClassFromString(@"UIRemoteKeyboardWindow")])
{
keyboardWindow = window;
}
}
if (![keyboardWindow.subviews containsObject:self.toolBarView]) {
[keyboardWindow addSubview:self.toolBarView];
[keyboardWindow addSubview:self.keyBoardInputView];
}
return keyboardWindow;
}
- (void)keybordShowAnimalByKeyboardHeight:(float)keyboardHeight AndAnimalTime:(float)keyboardDuration{
UIWindow *keyboardWindow = [self keyboardWindow];
float height = PCH_BitMap_BY_SIZE(90);
[self.toolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(keyboardWindow).mas_equalTo(-keyboardHeight);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(height);
}];
[self.keyBoardInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(keyboardWindow);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(keyboardHeight);
}];
if (self.toolBarView.height == 0) {// 第一次
return;
}
// 更新约束
[keyboardWindow layoutIfNeeded];
}
- (void)keybordHiddenAnimalByKeyboardHeight:(float)keyboardHeight AndAnimalTime:(float)keyboardDuration{
UIWindow *keyboardWindow = [self keyboardWindow];
// 修改为以前的约束(距下边距0)
[self.toolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
float height = PCH_BitMap_BY_SIZE(90);
make.bottom.equalTo(keyboardWindow).offset(height);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(height);
}];
[self.keyBoardInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.right.equalTo(keyboardWindow);
make.height.mas_equalTo(keyboardHeight);
}];
// 更新约束
[keyboardWindow layoutIfNeeded];
// [UIView animateWithDuration:keyboardDuration animations:^{
// }];
}