当键盘存在工作时,向上移动对话框,除非转动ipad

时间:2013-04-19 12:06:07

标签: ios objective-c ipad orientation modal-dialog

模式对话框在键盘出现时向上移动,在键盘消失时向下移动。

一切都很好,直到我旋转iPad。在除标准之外的任何其他方向,它都不起作用。当iPad转动时,模式对话框会在键盘显示时向下移动,而不是在键盘消失而不是向下时向上和向上移动。

这是我用来在键盘出现/消失时定位模态对话框的代码。

- (void)textFieldDidBeginEditing:(UITextField *)textField {


        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 140, self.view.superview.frame.size.width, self.view.superview.frame.size.height);

        }      
    }];

}


-(void)textFieldDidEndEditing:(UITextField *)textField {

    [UIView animateWithDuration:0.4 animations:^ {

        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 212, self.view.superview.frame.size.width, self.view.superview.frame.size.height);
        }
    }];

}

2 个答案:

答案 0 :(得分:1)

使用CGAffineTransformTranslate代替设置框架,例如:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,72);
    }      
}];
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
[UIView animateWithDuration:0.4 animations:^ {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,-72);
    }
}];
}

答案 1 :(得分:0)

您应该尝试使用键盘通知:

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

然后在选择器中调整帧。不在textFieldDidBeginEditing / textFieldDidEndEditing。

- (void)keyboardWasShown:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    keyboardHeight = MIN(keyboardSize.height, keyboardSize.width);
    // set new frame based on keyboard size 

- (void)keyboardWillBeDismissed: (NSNotification *) notification{
    [UIView animateWithDuration:0.4 animations:^{
    // original frame 
    }];
}