简体   繁体   English

键盘上方的空格隐藏文本字段

[英]Blank Space above keyboard hides the text field

I am getting problem when trying to move my scroll view up when the keyboard appears. 尝试在出现键盘时向上移动滚动视图时遇到问题。 The scroll view moves up in ios7 but in ios6 it doesn't and there is additional white space above the keyboard that hides the controls on the screen 滚动视图在ios7中向上移动,但在ios6中则没有,并且键盘上方还有额外的空白区域,用于在屏幕上隐藏控件

my code: 我的代码:

- (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;
}

Whats the problem here. 这是什么问题。 pls help 请帮助

The problem is, that you only change size of the scroll view, but you don't tell the scrollview to scroll to the text field. 问题是,您仅更改滚动视图的大小,但不告诉滚动视图滚动到文本字段。

Look at my method scrollToEditingTextField for better understanding. 查看我的方法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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM