Latest 1.2.0
Homepage https://github.com/Azureblade3808/KMSOrientationPatch
License Apache License, Version 2.0
Platforms ios 8.0
Frameworks UIKit
Authors

Motivation

In iOS APPs, a view controller can provide which interface orientations it supports, and view controllers may have different supported interface orientations between each other.

When a view controller is presented or dismissed, the whole UI will probably rotate, letting the active view controller lay in one of its supported interface orientations.

But if a view controller is pushed or popped within a navigation controller, things get somehow different. Firstly, the active view controller is the navigation controller instead of one of the view controller in it, so the orientation of UI is determined by the navigation controller’s supported interface orientation. Still, even if we have codes that bind the navigation controller’s supported interface orientation with its top view controller’s, UI will not rotate upon pushing or popping. Simply speaking, you will almost definitely fail to push a view controller which doesn’t support current interface orientation, then expect the UI to rotate automatically.

Normally, one would be told to add codes like

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

to deal with such situations. This hack may accomplish something, but leaves unreadable codes here and there. More importantly, using it tend to lead to odd UI rotations afterwards.

So comes this Framework KMSOrientationPatch.

Usage

After importing it into your project, you can simply call

UIDevice.attemptRotationToSupportedOrientations()

in Swift, or

[UIDevice attemptRotationToSupportedOrientations];

in Objective-C at any time on main thread, to let UI rotate to the closest supported orientation.

You may add codes like below into your base view controller and no longer need to think about rotations.

override
func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    UIDevice.attemptRotationToSupportedOrientations()
}

Importing

  • At this point, copying the file UIDevice+Extensions.swift into your project is sufficient for it to work.

  • Or you can add the project KMSOrientationPatch into your workspace as a Framework project.

  • At last, you can use it as a pod by adding next line into your pod file.
    pod 'KMSOrientationPatch'

Latest podspec

{
    "name": "KMSOrientationPatch",
    "version": "1.2.0",
    "summary": "An extension of UIDevice to allow interface rotations upon changes of supported interface orientations.",
    "homepage": "https://github.com/Azureblade3808/KMSOrientationPatch",
    "license": {
        "type": "Apache License, Version 2.0",
        "file": "LICENSE"
    },
    "authors": {
        "Azureblade3808": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/Azureblade3808/KMSOrientationPatch.git",
        "tag": "v1.2.0"
    },
    "source_files": "KMSOrientationPatch/Codes/**/*",
    "frameworks": "UIKit"
}

Pin It on Pinterest

Share This