Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hi all,

I'm using this calculate to determine the 'new' vertical position for my view, just so that the textfield will be above my keyboard.

Code:
if(moveView) {
    int toPoint = accountVieww.frame.origin.y + accountScoll.frame.origin.y + accountContent.frame.origin.y + activeField.frame.origin.y + activeField.frame.size.height;
    int keyboardPoint = self.view.frame.size.height - keyboardSize.height;
    
    if(toPoint > keyboardPoint) {
        
        int theDiff = abs(toPoint - keyboardPoint);
    }
}

The integer theDiff contains the vertical difference between the bottom of current textfield and the top of the keyboard.
It's working great! xD

However, in my case, the textfield is inside an scrollview.
And so when I scrolled a bit down, the difference is incorrect and you can guess why.

How can I get the current amount I scrolled up ?

Just for example, I have a scrollview of 500 high, and a content of 700.
So I can scroll up to 200 down.
How can I know that I scrolled only like.. 100 down?
If I know this then I can substract this from my current calculated toPoint, and it will be all perfect.

Thanks for any advice!

p.s. accountContent == UIView inside scrollview, to make it easier for me to set the scrollview contentSize ;-)
all textfields etc are in the accountContent

-----

Just edited:

theDiff -= accountScroll.contentOffset.y;

and works like a charm.

Full code down here:
Maybe someone else will find it usefull
Code:
UITextField *activeField;
BOOL keyboardIsShown = NO;
BOOL movedView = NO;

-(void)viewWillAppear:(BOOL)animated {
    // register for keyboard notifications
    [[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];
    keyboardIsShown = NO;
    movedView = NO;
    // Do any additional setup after loading the view.
}

-(void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}



- (void)keyboardWillHide:(NSNotification *)n {
    if(movedView) {
        CGRect viewFrame = self.view.frame;
        viewFrame.origin.y = 0;
    
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [self.view setFrame:viewFrame];
        [UIView commitAnimations];
        
        movedView = NO;
    }
    keyboardIsShown = NO;
}

- (void)keyboardWillShow:(NSNotification *)n {
    if (keyboardIsShown)
        return;
    
    NSDictionary* userInfo = [n userInfo];
    
    // get the size of the keyboard
    CGRect keyboard = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGSize keyboardSize = [self.view convertRect:keyboard fromView:self.view.window].size;
    
    
    //Get the lowest part from the bottom of the textfield (Y coordinate)
    int toPoint = accountVieww.frame.origin.y + accountScroll.frame.origin.y + accountContent.frame.origin.y + activeField.frame.origin.y + activeField.frame.size.height;
    //Get the highest point of the keyboard (on screen, Y coordinate)
    int keyboardPoint = self.view.frame.size.height - keyboardSize.height;

    
    if(toPoint > keyboardPoint) {
        int theDiff = abs(toPoint - keyboardPoint);
        //Remove the extra offset from the scrollview
        theDiff -= accountScroll.contentOffset.y;
        
        CGRect viewFrame = self.view.frame;

        //Move the main view to the difference
        viewFrame.origin.y -= theDiff;
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [self.view setFrame:viewFrame];
        [UIView commitAnimations];
        movedView = YES;
    }
    keyboardIsShown = YES;
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.