webview键盘自适应_给webview自定义键盘样式

需求:给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:^{

// }];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值