当键盘出现时尝试移动滚动视图时出现问题。 滚动视图在ios7中向上移动,但在ios6中没有,并且键盘上方还有额外的空白区域隐藏屏幕上的控件
我的代码:
- (void)viewWillAppear:(BOOL)animated
{
[super viewDidAppear:animated];
keyboardIsShown = NO;
[super viewWillAppear:animated];
[[self view] endEditing:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
}
- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the noteView
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height -= (keyboardSize.height - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
scrollView.contentSize = formView.frame.size;
keyboardIsShown = YES;
}
这里的问题是什么。请帮忙
答案 0 :(得分:1)
问题是,您只更改了滚动视图的大小,但是您没有告诉滚动视图滚动到文本字段。
查看我的方法 scrollToEditingTextField 以便更好地理解。
- (void)keyboardWillShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize beginSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// keyboard will appear
if(!_keyboardShowed) {
_keyboardShowed = YES;
[UIView animateWithDuration:0.4
animations:^{
CGFloat scrollHeight =_scrollView.size.height - (IS_PORTRAIT_ORIENTATION ? beginSize.height : beginSize.width);
CGRect scrollFrame = _scrollView.frame;
scrollFrame.size.height = scrollHeight;
_scrollView.frame = scrollFrame;
}
completion:^(BOOL finished){ }];
}
[self scrollToEditingTextField];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize endSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
// keyboard will disappear
if(_keyboardShowed) {
CGFloat scrollHeight =_scrollView.size.height + (IS_PORTRAIT_ORIENTATION ? endSize.height : endSize.width);
CGRect scrollFrame = _scrollView.frame;
scrollFrame.size.height = scrollHeight;
_scrollView.frame = scrollFrame;
_keyboardShowed = NO;
}
}
-(void)scrollToEditingTextField {
// find which text field is currently editing
UITextField *editingTextFiled = [self editingTextField:self.thisView];
if(editingTextFiled == nil) return; // text field didnt found
CGPoint scrollPoint = [editingTextFiled convertPoint:CGPointMake(0, 0) toView:self.scrollView];
scrollPoint.y -= 70;
scrollPoint.x = 0;
[_scrollView setContentOffset:scrollPoint animated:YES];
}
-(UITextField*)editingTextField:(UIView*)view
{
if( ( [[view class] isSubclassOfClass:[UITextField class]] ||
[[view class] isSubclassOfClass:[UITextView class]] ) &&
[view isFirstResponder] ) {
return (UITextField*)view;
}
for(UIView *subview in view.subviews ) {
if([[subview class] isSubclassOfClass:[UISearchBar class]]) {
return nil;
}
if( ( [[subview class] isSubclassOfClass:[UITextField class]] ||
[[subview class] isSubclassOfClass:[UITextView class]] ) &&
[subview isFirstResponder] ) {
return (UITextField*)subview;
}
}
// recursion
for(UIView *subview in view.subviews ) {
UITextField *textField = [self editingTextField:subview];
if(textField) return textField;
}
return nil;
}