我知道有很多类似的解决方案,比如TPKeyboardAvoiding、苹果著名的解决方案以及各种建议涉及使用UIScrollView。在我的情况下,我需要调整一个视图来适应键盘而不是滚动或移动它。这个解决方案最接近我想要实现的效果,因此它是我的基础。然而,我在横屏模式下遇到了问题。当键盘出现时,我的方法会调整视图大小,代码如下:
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[self textField].superview convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect statusBarFrame = [[self textField].superview convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
CGRect bounds = [self textField].superview.bounds;
CGRect newFrame = CGRectMake(0.0, 0.0, bounds.size.width, keyboardFrame.origin.y + statusBarFrame.size.height);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
[self textField].superview.frame = newFrame;
} completion:nil];
}
这在竖屏模式下完美运行。
很明显,我在使用坐标时出现了问题,某些参考框架在横向模式下并不是我所认为的那样,但我很难弄清楚如何解决它。我尝试使用-convertRect:将所有东西转换成各种形式,但是我试过的任何方法都没有帮助我。
我真的希望有人能够比我更清楚地理解所有这些矩形以及它们在方向改变时如何变化,找出我做错了什么,以及我需要做什么才能解决这个问题。供参考,我已经创建了一个项目,展示了我遇到的问题的最简单情况。
curve
的使用实际上是不正确的。请参考此线程:https://dev59.com/CGMk5IYBdhLWcg3wyw_H#18975862 - phatmann