Uitableview iOS 8 UIView在键盘出现时不向上移动
我正在开发一个聊天应用程序,它有Uitableview iOS 8 UIView在键盘出现时不向上移动,uitableview,uiview,keyboard,uitextfield,ios8,Uitableview,Uiview,Keyboard,Uitextfield,Ios8,我正在开发一个聊天应用程序,它有UITableView和UIView,其中包含UITextField和UIButton。当键盘出现时,我使用以下代码向上移动ui视图 (void)keyboardWillShow:(NSNotification *)notification { NSDictionary* info = [notification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBegin
UITableView
和UIView
,其中包含UITextField
和UIButton
。当键盘出现时,我使用以下代码向上移动ui视图
(void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.2f animations:^{
CGRect frame = self.inputView.frame;
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
frame.origin.y -= kbSize.width;
else
frame.origin.y -= kbSize.height;
self.inputView.frame = frame;
;
}];
}
此代码在iOS 7之前工作正常,但在iOS 8中,键盘上方不显示视图
有人能提出可能的问题吗,或者在iOS 8中是否有任何更改?您的代码似乎是正确的,但我更喜欢使用UIKeyboardDidChangeFrameNotification或UIKeyboardWillChangeFrameNotification,因为当看到键盘时,这些将告诉您当预测文本栏上升或下降时键盘框架的变化 在你的视图中添加这个
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameDidChange:)
name:UIKeyboardDidChangeFrameNotification object:nil];
然后将此方法粘贴到ViewController中
-(void)keyboardFrameDidChange:(NSNotification*)notification{
NSDictionary* info = [notification userInfo];
CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[yourView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y-yourView.frame.size.height, 320, yourView.frame.size.height)];
}
这将处理您所有的键盘情况,例如当它向上或向下或在其框架中使用预测文本栏进行更改时
当您离开视图时,还可以删除observer。您可以使用附件视图,它将自身连接到键盘顶部。或者,如果您想在定制上获得更多功能,可以使用@pankaj_wadwha解释的通知来获取帧信息。额外好处:您还可以获得动画信息(如速度),这样您的视图可以完美地沿着键盘移动。公认的答案几乎是正确的。要使视图的动画与键盘的动画相匹配,您需要使用UIKeyboardWillChangeFrameNotification而不是UIKeyboardDidChangeFrameNotification。这样,您启动的动画将与键盘的动画精确匹配。这里有一些代码来完成整个过程。我使用键盘的动画来驱动autolayout约束常量的动画,但您可以轻松地将其调整为整个图幅的动画。(注意,我们必须使用老式的动画来连接UIKeyboardCurveInfo键,该键提供与键盘动画完全匹配的动画曲线 在视图中加载:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameDidChange:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
在ViewController中:
- (void)keyboardFrameDidChange:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat height = kKeyBoardFrame.size.height;
[self.view removeConstraints:self.verticalButtonConstraints];
NSDictionary *metrics = @{@"height" : @(height)};
NSDictionary *views = @{@"nextButton" : self.nextButton};
self.verticalButtonConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V: [nextButton(52)]-(height)-|" options:0 metrics:metrics views:views];
[self.view addConstraints:self.verticalButtonConstraints];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
[self.view layoutIfNeeded];
[UIView commitAnimations];
}我刚刚遇到了这个问题,并发现了一个我想与大家分享的发现。在iOS 8中,每当键盘即将出现或即将消失时,主视图的子视图的布局过程都将完成。这些过程在iOS 7上无法完成。因此,如果您尝试在keyBoardWillShow或keyboardWillChangeFrame中设置主视图的子视图的动画,则布局过程将撤消动画,您尝试设置动画的子视图将移回其原始位置。这就是为什么keyboardDidChangeFrame可以为子视图设置动画,而keyboardWillChangeFrame不能 我还注意到了一些奇怪的事情,就是这些调用的时间。似乎在应用程序启动后键盘第一次出现时,对keyboardDidChangeFrame的调用来得太晚,无法用键盘进行动画处理,所以它们一起向上滑动,但在第二次和随后的键盘显示时,对键盘的调用DidChangeFrame发生得更快,而且看起来您实际上可以通过键盘设置视图的动画
我必须注意,我正在使用C#和Xamarin作为我的iOS开发平台,因此使用Swift或Obj-C时可能会有所不同。“y”这里没有compile@ngb你到底在说什么?@pankajwadhwa在我的情况下什么都没有发生。我有一个固定在底部空间的视图。我尝试了你上面的代码。我正在获取kKeyBoardFrame的值。但是[yourView setFrame:…]不起作用。可能是什么原因?谢谢。