Keyboard Scroll on Active Text Field - Scrolling to Out of View?

前端 未结 4 2037
庸人自扰
庸人自扰 2020-12-13 16:35

I have an application with a view that has text fields from the top of the view to the bottom of the view. I needed it to scroll when editing the bottom fields so that the f

相关标签:
4条回答
  • 2020-12-13 17:01

    Since I found it, I use TPKeyboardAvoiding

    It is working great, and is very easy to setup:

    1. Add a UIScrollView into your view controller's xib
    2. Set the scroll view's class to TPKeyboardAvoidingScrollView (still in the xib, via the identity inspector)
    3. Place all your controls within that scrollview

    It also automatically hooks up "Next" buttons on the keyboard to switch through the text fields.

    Good luck!

    0 讨论(0)
  • 2020-12-13 17:01

    @Minumaster's approach works fine but I would like to suggest a much simpler yet generalised way just like apple does by takes keypads's height into consideration which is greatly helpful when we are using custom toolbar on top of keyboard. though apple's approach here has got few issues.

    Here is my approach (Slightly modified apple's way) -

    // Called when the UIKeyboardDidShowNotification is sent.
    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    }
    
    // Called when the UIKeyboardWillHideNotification is sent
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    }
    
    0 讨论(0)
  • 2020-12-13 17:06

    After making sure the delegate was set, I went through the code and drew out all of the coordinates of everything and did all of the math and had to manually set some variables, but now it works as intended.

    If someone needs help with it in the future I'll be glad to help. My Twitter is in my profile.

    0 讨论(0)
  • 2020-12-13 17:07

    UPDATE: Below answer is outdated. For now you can use "TPkeyboardavoiding" library for handle all kind of text field operation in UIScrollView, UITableView & many more. Try it and let me know if any one have a problem in integration.

    Old Answer

    There is no need to register for the keyboard notifications for this functionality. In order to center the active textField the screen above the keyboard, you only have to set the contentOffset of the UIscrollView two methods as shown here:

    // called when textField start editting.
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        activeField = textField;
        [scrollView setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES];
    }
    
    
    
    // called when click on the retun button.
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    
    
        NSInteger nextTag = textField.tag + 1;
        // Try to find next responder
        UIResponder *nextResponder = [textField.superview viewWithTag:nextTag];
    
        if (nextResponder) {
            [scrollview setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES];
            // Found next responder, so set it.
            [nextResponder becomeFirstResponder];
        } else {
            [scrollview setContentOffset:CGPointMake(0,0) animated:YES];
            [textField resignFirstResponder];   
            return YES;
        }
    
        return NO; 
    }
    

    Note: All TextField should have incremental tags Like 1,2,3 and so no. And set delegates to self.

    Thanks,

    0 讨论(0)
提交回复
热议问题