Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent keyboard from overlapping UIButton / UITextField

I've been reading the Text Programming Guide provided by Apple via the following web address:

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

I am trying to prevent buttons and textFields from being overlapped by keyboards.

When I paste the code from listing 5-1 (listed below) as per the instruction of the guide I receive errors such as :

" Use of unidentified identifier 'scrollView'; Did you mean 'UIScrollView' ? "

and

" Property 'contentsInset' not found in 'scrollView' "



// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(keyboardWasShown:)
            name:UIKeyboardDidShowNotification object:nil];

   [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(keyboardWillBeHidden:)
             name:UIKeyboardWillHideNotification object:nil];

}

// 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);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}
like image 709
arman Avatar asked Mar 13 '14 06:03

arman


2 Answers

Try this code will resolve your problem, replace TextView by TextField

-(void)textViewDidBeginEditing:(UITextView *)textView{

    [self animateTextFild:textView up:YES];
}

-(void)textViewDidEndEditing:(UITextView *)textView{

    [self animateTextFild:textView up:NO];
    [textView resignFirstResponder];
}

-(void) animateTextFild:(UITextView *)textField up:(BOOL)up{

    int animatedDistance;

    int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;

    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {

        animatedDistance = 216-(730-moveUpValue);
    }
    else
    {
        animatedDistance = 162-(520-moveUpValue);
    }

    if(animatedDistance>0)
    {
        const int movementDistance = animatedDistance;

        const float movementDuration = 0.3f;

        int movement = (up ? -movementDistance : movementDistance);

        [UIView beginAnimations: nil context: nil];

        [UIView setAnimationBeginsFromCurrentState: YES];

        [UIView setAnimationDuration: movementDuration];

        myView.frame = CGRectOffset(ViewEdit.frame, 0, movement);

        [UIView commitAnimations];
    }
}
like image 122
Dattatray Deokar Avatar answered Oct 13 '22 07:10

Dattatray Deokar


The below method will help you.

Declare the following constants in your .m file.

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION     = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION     = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT    = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT   = 162;

And add the following code

 - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        CGRect textFieldRect =
        [self.view.window convertRect:textField.bounds fromView:textField];
        CGRect viewRect =
        [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =
    midline - viewRect.origin.y
    - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator =
    (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
    * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame = self.scrollView.frame;
    viewFrame.origin.y -= animatedDistance;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.scrollView setFrame:viewFrame];

    [UIView commitAnimations];
    for (id navigBar in [self.view subviews])
    {
        if ([navigBar isKindOfClass:[UINavigationBar class]])
        {
            [self.view bringSubviewToFront:navigBar];
        }
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect viewFrame = self.scrollView.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.scrollView setFrame:viewFrame];

    [UIView commitAnimations];
    for (id navigBar in [self.view subviews]) {

        if ([navigBar isKindOfClass:[UINavigationBar class]])
        {
            [self.view bringSubviewToFront:navigBar];
        }
    }
}
like image 30
Nithin Michael Avatar answered Oct 13 '22 07:10

Nithin Michael