iOS 8 UITextView在键盘显示时不滑动
问题描述:
为了避免键盘隐藏文本,我使用了以下代码,这在以前版本的iOS中工作得很好。在iOS 8中没有任何反应。iOS 8 UITextView在键盘显示时不滑动
- (BOOL)textViewShouldBeginEditing:(UITextView*)textView {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect frame = self.view.bounds;
frame.size.height = frame.size.height - [Application keyboardHeight];
noteView.frame = frame;
[UIView commitAnimations];
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
noteView.frame = self.view.bounds;
[UIView commitAnimations];
}
也许我错过了一些愚蠢的东西,但我不明白发生了什么事。
答
我以不同的方式解决这个问题,它在iOS8中似乎运行良好。这个例子将看到的UITextView的占用可用空间
- (void) viewDidLoad
{
//Do other stuff
// listen for keyboard
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
然后是2层的方法来处理显示和隐藏键盘
- (void) keyboardWillShow:(NSNotification *) notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGSize size = self.view.frame.size;
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
textView.frame = CGRectMake(0, 0, size.width , size.height - keyboardSize.height + 50) ;
else
textView.frame = CGRectMake(0, 0, size.width , size.height - keyboardSize.width + 50) ;
}
- (void) keyboardWillHide:(NSNotification *) notification {
#pragma unused(notification)
textView.frame = CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height) ;
}
不知道这是答案,但张贴的答案如此代码被格式化。希望它能帮助你一切。
答
我不确定你的[应用程序keyboardHeight]返回什么,但我刚刚有类似的问题,我找出了问题所在。当使用@RobCroll使用的相同通知方法时,您必须在iOS7及更早版本中使用keyboardSize.width作为横向方向。但在iOS8中,Apple最终修复了这个错误,并且您还必须使用keyboardSize.height进行横向设置!也许你的[Application keyboardHeight]也仍然使用keyboardSize.width?