iPad - 目标 - C - 键盘 - 一切都从左向右移动,而不是向下移动

时间:2014-12-22 22:13:45

标签: ios objective-c ipad

我希望这个问题有意义,我有一个登录屏幕,当我点击我的输入字段(用户名或密码)时,会出现一个键盘。但当发生这种情况时,一切都会从左向右移动,所以左边有一个很大的空白区域。我期待的是一切都从下到上移动。我真的希望这是有道理的。这是我的代码:

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

如果有人能提供帮助,那就太棒了。我的项目设备仅设置为横向右侧。

1 个答案:

答案 0 :(得分:3)

之前我遇到过相同的问题,在iOS 7和8中它的工作方式不同。所以我实现了一个处理问题的方法:

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

你可以这样称呼:

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