我发现这个问题的解决方案涉及几个步骤:
1) 我发现有必要相对于屏幕中心更改模式的中心(分配给 self.myNavController.view.superview.center)。我根据 [UIScreen mainScreen].bounds.size 计算了屏幕的中心。对于一些示例代码,我使用了下面的方法 screenCenter。
// Adapted from: http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8
+ (CGSize) screenSize;
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGSize rotatedSize;
if ([UIDevice ios7OrEarlier] && [[SMRotation session] isLandscape]) {
rotatedSize = CGSizeMake(screenSize.height, screenSize.width);
}
else {
rotatedSize = screenSize;
}
return rotatedSize;
}
+ (CGPoint) screenCenter;
{
CGSize size = [self screenSize];
CGPoint center = CGPointMake(size.width/2.0, size.height/2);
return center;
}
2) 现在,假设您已经计算了必须向上移动模态的量(例如,给定键盘高度和模态高度以及模态上文本字段的位置),将此量称为 dy。接下来我发现如果应用程序处于反向旋转(倒置纵向或横向),则有必要在将 dy 的符号应用于我正在计算的 CGPoint 中心位置之前更改它的符号。像这样的:
CGPoint newCenter = [SMRotation screenCenter];
if ([SMRotation session].isInverted) {
dy = -dy;
}
newCenter.y += dy;
这里有一些 isInverted 的代码:
- (BOOL) isInverted;
{
switch (self.interfaceOrientation) {
case UIInterfaceOrientationPortraitUpsideDown:
case UIInterfaceOrientationLandscapeRight:
return YES;
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationUnknown:
return NO;
}
}
3) 然后,如果应用程序是横向的,我发现有必要交换 x 和 y 坐标。像这样的:
if ([SMRotation session].isLandscape) {
newCenter = CGPointMake(newCenter.y, newCenter.x);
}
4 最后,我完成了更新模态中心的任务:
self.myNavController.view.superview.center = newCenter;