我可以处理两个事件:键盘显示时和键盘隐藏时。在 iOS 8.2 及更早版本中一切正常。
但是当你改变你的键盘语言时如何处理事件呢?当您将英文键盘更改为 emoji 键盘时,emoji 键盘的高度(在 ios 8.3 中)更大并且隐藏了内容。
或者你有一个如何控制 iOS 8.3 表情符号键盘高度的解决方案?
我可以处理两个事件:键盘显示时和键盘隐藏时。在 iOS 8.2 及更早版本中一切正常。
但是当你改变你的键盘语言时如何处理事件呢?当您将英文键盘更改为 emoji 键盘时,emoji 键盘的高度(在 ios 8.3 中)更大并且隐藏了内容。
或者你有一个如何控制 iOS 8.3 表情符号键盘高度的解决方案?
好的。所以看着我的旧代码,我记得,我不使用 2 个观察者(UIKeyboardDidShowNotification
/ UIKeyboardDidHideNotification
)。我使用单个观察者 ( UIKeyboardWillChangeFrameNotification
),它会触发每个事件:键盘隐藏、键盘显示、键盘更改框架。
在我的情况下,文本框和发送按钮嵌套在 a 中UIView
,这是视图添加在view
中UIViewController
,高于其他所有内容。
我viewDidAppear
在 . 中添加观察者并删除观察者viewWillDisappear
(以避免在视图不活动时触发任何通知)。
以上信息对于您的情况不是必需的,只是为了提供信息而添加。相关代码如下:
添加观察者:
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
处理通知:
- (void) keyboardWillChangeFrame:(NSNotification*)notification {
NSDictionary* notificationInfo = [notification userInfo];
CGRect keyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:[[notificationInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[[notificationInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]
animations:^{
CGRect frame = self.textViewContainer.frame;
frame.origin.y = keyboardFrame.origin.y - frame.size.height;
self.textViewContainer.frame = frame;
} completion:nil];
}
您可能需要对该frame.origin.y...
行进行一些调整才能正确计算。我不知道你是否有一个UITabBarController
或任何底部的酒吧。这里最安全的赌注是:
frame.origin.y = self.view.frame.size.height - keyboardFrame.size.height - X;
如果X
您的 VC 覆盖整个屏幕,则为 0。如果没有,请使用任何底栏的高度。
我有同样的问题。只需将 UIKeyboardFrameBeginUserInfoKey 替换为 UIKeyboardFrameEndUserInfoKey 。:-)
它对我有用。
值得注意的是,表情符号键盘与启用建议文本的标准键盘高度相同。
要正确确定键盘高度并调整视图,请添加以下观察者:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
然后我使用以下方法对键盘调整进行动画处理。实际上,您需要的只是keyboardBounds
对象,但如果您碰巧使用了 AutoLayout,您可以这样做:
- (void)keyboardDidShow:(NSNotification *)notification
{
[self scrollControlBarTo:notification up:YES];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self scrollControlBarTo:notification up:NO];
}
- (void)scrollControlBarTo:(NSNotification *)notification up:(BOOL)up
{
[_keyboardControlsBar layoutIfNeeded];
CGRect keyboardBounds;
NSDictionary *info = [notification userInfo];
NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
double duration = [number doubleValue];
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
UIViewAnimationCurve curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[UIView setAnimationCurve:curve];
_keyboardControlsBarBottomConstraint.constant = (up) ? keyboardBounds.size.height : 0;
[self.view layoutIfNeeded];
} completion:nil];
}
上面的代码,但在swift中:
func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
...
func keyboardWillChangeFrame(_ notification: Notification?) {
let notificationInfo = notification?.userInfo
let keyboardFrame = notificationInfo?[UIResponder.keyboardFrameEndUserInfoKey]?.cgRectValue
UIView.animate(withDuration: TimeInterval((notificationInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.floatValue ?? 0.0), delay: 0, options: UIView.AnimationOptions(rawValue: (notificationInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber)?.intValue ?? 0), animations: {
let frame = self.textViewContainer.frame
frame.origin.y = (keyboardFrame?.origin.y ?? 0.0) - frame.size.height
self.textViewContainer.frame = frame
})
}