user979331
user979331

Reputation: 11911

iPad - Objective - C - Keyboard - Everything moves from left to right, not down to up

I hope this question makes sense, I have a login screen, when I click on the one of my input fields (either Username or Password) a keyboard appears. but when that happens Everything moves in motion from left to right, so I have a big white space on the left side. What I am expecting is for everything to move from down to up instead. I really hope this makes sense. Here is my code:

- (void)animateTextField:(UITextField *) textField up: (BOOL) up;
- (void)NavButtonPressed: (UIButton*)sender;

- (void)NavButtonPressed: (UIButton*)sender
{
    if([sender isEqual:navBarNextButton])
    {
        [self.idTextField resignFirstResponder];
        [self.passwordTextField becomeFirstResponder];
        currentResponder = 1;
        [self animateTextField:self.passwordTextField up:NO];
        [self animateTextField:self.passwordTextField up:YES];
        [navBarNextButton setEnabled:NO];
        [navBarPrevButton setEnabled:YES];

    }
    else
    {
        [self.passwordTextField resignFirstResponder];
        [self.idTextField becomeFirstResponder];
        currentResponder = 0;
        [self animateTextField:self.idTextField up:NO];
        [self animateTextField:self.idTextField up:YES];
        [navBarNextButton setEnabled:YES];
        [navBarPrevButton setEnabled:NO];
    }
}

- (void)animateTextField:(UITextField *) textField up: (BOOL) up
{
    int movementDistance;
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        if([textField isEqual:self.idTextField])
        {
            [textfieldNavigatorView setFrame:CGRectMake(0.0f, (1024.0f/2.0f)-31.5f, 1024.0f, 45.0f)];
            movementDistance = 110;
        }
        else
        {
            [textfieldNavigatorView setFrame:CGRectMake(0.0f, (1024.0f/2.0f)-26.5f, 1024.0f, 45.0f)];
            movementDistance = 115;
        }
    }
    else
    {movementDistance = 0;}

    const float movementDuration = 0.3f;
    int movement=0;
    if([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft)
        movement = (up? -movementDistance : movementDistance);
    if([self interfaceOrientation] == UIInterfaceOrientationLandscapeRight)
        movement = (up? movementDistance : -movementDistance);

    [UIView beginAnimations:@"keyboardtransition" context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, movement, 0);

    [UIView commitAnimations];
}

If anyone could help, that would be amazing. My project Device is set to Landscape Right only.

Upvotes: 1

Views: 86

Answers (1)

Midhun MP
Midhun MP

Reputation: 107201

I had same issue before, in iOS 7 and 8 it works differently. So I implemented a method for handling the issue:

- (void)keyboardHideShow:(UIView *)view shiftBy:(float)shiftAmount forState:(BOOL)state
{
    float keyBoardOffset = shiftAmount;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGRect rect = [[[UIApplication sharedApplication] keyWindow] bounds];
    if (state)
    {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8)
        {
            if (orientation == UIInterfaceOrientationLandscapeLeft)
            {
                rect.origin.x -= keyBoardOffset;
            }
            else
            {
                rect.origin.x += keyBoardOffset;
            }
        }
        else
        {
            rect.origin.y -= keyBoardOffset;
        }
    }
    view.frame = rect;
    [UIView commitAnimations];
}

You can call it like:

[self keyboardHideShow:textfieldNavigatorView shiftBy:31.5 forState:true];  // For up
[self keyboardHideShow:textfieldNavigatorView shiftBy:31.5 forState:false]; // For down

Upvotes: 3

Related Questions