Need help setting up an interface where rotation is fixed for most elements, but alert and sharing boxes auto-rotate with the device

折月煮酒 提交于 2019-12-01 23:53:32

Got it working. I needed to access presentedViewController.view to rotate the alert and share views.

//used to periodically trigger a check of orientation
var updateTimer: NSTimer?

//Checks if the device has rotated and, if so, rotates the controls. Also prompts the user that portrait orientation is bad.
func checkOrientation(timer: NSTimer) {
    //Array of the views that need to be rotated as the device rotates
    var viewsToRotate = [oneView, anotherView]

    //This adds the alert or sharing view to the list, if there is one
    if let presentedVC = presentedViewController?.view {
        viewsToRotate.append(presentedVC)
    }

    //Rotate all of the views identified above
    for viewToRotate in viewsToRotate {
        switch UIDevice.currentDevice().orientation {
        case UIDeviceOrientation.Portrait:
            viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
        case UIDeviceOrientation.PortraitUpsideDown:
            viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
        case UIDeviceOrientation.LandscapeRight:
            viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(2 * M_PI_2))
        default:
            viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(0))
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!