【问题标题】:Moving UIVIewController modal up when keyboard appears independent of rotation with iOS7当键盘出现独立于 iOS7 的旋转时,向上移动 UIVIewController 模式
【发布时间】:2015-07-02 23:11:47
【问题描述】:

我只是为此苦苦挣扎了一段时间,所以将其记录下来供其他人使用。

这是我遇到的问题。使用支持 iOS7 的 iPad 应用程序,我有一个模态视图控制器,它在模态底部附近有一个文本字段。因此,当键盘出现时,我想将该模式向上移动,以便在键盘存在的情况下仍然可以看到文本字段。对于 iOS8,这个问题有一个非常干净的解决方案(例如,参见 Moving a modally presented UIViewController up when keyboard appears on iPad with iOS8)。在 iOS7 中,我使用 self.myNavController.view.superview.center 进行重新定位,但考虑到键盘的外观,在尝试移动模式时遇到了问题。我使用的坐标 CGPoint 调整不会在 iPad 的所有四个旋转/方向上将模态移动到正确的方向。

问题部分在于 iOS7 是如何进行旋转的——使用变换。但是,我无法使用 CGPointApplyAffineTransform 或使用视图转换点(例如 convertPoint:fromView:) 来解决问题。

【问题讨论】:

    标签: objective-c ipad ios7 autorotate


    【解决方案1】:

    我发现这个问题的解决方案涉及几个步骤:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-31
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      相关资源
      最近更新 更多