我可以处理两个事件:键盘显示和键盘隐藏时。 在iOS 8.2及更早版本中,一切都运行良好。
但是如何在更改键盘语言时处理事件?当您将英文键盘更改为表情符号键盘时,表情符号键盘的高度(在ios 8.3中)更大,并且隐藏了内容。
或许您有一个解决方案如何控制iOS 8.3表情符号键盘高度?
答案 0 :(得分:12)
行。所以看着我的旧代码,我记得,我不使用2个观察者(UIKeyboardDidShowNotification
/ UIKeyboardDidHideNotification
)。我使用了一个观察者(UIKeyboardWillChangeFrameNotification
),它触发了每个事件:键盘隐藏,键盘显示,键盘更换框架。
在我的情况下,文本框和发送按钮嵌套在UIView
中,此视图已添加到view
的{{1}}中,高于其他所有内容。
我在UIViewController
中添加观察者并删除viewDidAppear
中的观察者。(以避免在视图未激活时触发任何通知)。
以上信息对您的案例不是必需的,只是为了提供信息而添加。相关代码如下:
ADD OBSERVER:
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
如果您的VC涵盖整个屏幕,frame.origin.y = self.view.frame.size.height - keyboardFrame.size.height - X;
为0。如果没有,请使用任何底栏的高度。
答案 1 :(得分:6)
我遇到了同样的问题。只需用UIKeyboardFrameEndUserInfoKey替换UIKeyboardFrameBeginUserInfoKey即可。 : - )
它对我有用。
答案 2 :(得分:4)
值得注意的是表情符号键盘与启用建议文字的标准键盘相同。
要正确确定键盘高度并调整视图,请添加以下观察者:
[[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];
}
答案 3 :(得分:0)
上面的代码,但迅速:
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
})
}