I am trying to create a view that doesn't rotate with device orientation. I want just this one view to not rotate because I have a UIToolbar that I don't want to rotate. Is this possible? If so, how do I implement it? I am using Swift. Also, is it possible to rotate the UIBarButtonItems with device orientation on the UIToolbar? If so, how do I implement that? Just to restate: I want the view and toolbar within it to not rotate with orientation; I want the buttons on the toolbar to rotate with orientation. Thanks.
-
1Possible duplicate of [Disabling auto rotation for a UIView](https://stackoverflow.com/questions/3301343/disabling-auto-rotation-for-a-uiview) – Damiaan Dufaux Jun 02 '17 at 16:45
-
@devian It's not a duplicate because I do not want my toolbar to rotate with orientation. I want the buttons on the toolbar to rotate with orientation. Also, I don't believe that question is written in Swift. – TonyStark4ever Jun 02 '17 at 17:23
5 Answers
Per the View Controller Programing Guide
If you want to temporarily disable automatic rotation, avoid manipulating the orientation masks to do this. Instead, override the shouldAutorotate variable on the initial view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.
So you need to subclass 'UINavigationController', implement shouldAutorotate and use your navigation controller class in your storyboard.
Swift 3
override var shouldAutorotate: Bool {
let currentViewController = self.topViewController
return !currentViewController.isKind(of: DetailViewController.classForCoder())
}

- 2,386
- 1
- 22
- 33
-
So for this view, I would need to embed in a navigation controller on it, but then how do I subclass it? I am pretty new to code. I already have a .swift file (Main View Controller.swift) that's separate from my other view controller files for this view. I have this so far: `import UIKit class MainViewController: UIViewController { @IBOutlet var mainView: UIView! override func viewDidLoad() { super.viewDidLoad() } }` Where do I put the code you have provided, and what other code is necessary? Thanks. – TonyStark4ever Jun 02 '17 at 18:07
Why not just rotate the view in question -90 degrees or +90 degrees as required? I'll happily provide example code if you'd like. Edit: I was suggesting something like this (but look at the caveat after the code):
let degrees = 90.0 // or -90, 180 depending on phone's movement.
let rotatedView: UIView = <yourOriginalView>
rotatedView.transform = CGAffineTransform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI / 180))
In iOS 8.0 and later, the transform property does not affect Auto Layout. Auto layout calculates a view’s alignment rectangle based on its untransformed frame. -- So this might not work for your purposes if you're using autolayout.... And I admit this is a kludge.

- 4,958
- 6
- 36
- 56
-
Can you provide the code? I'm not exactly sure what you mean, but I think once I see the code, I will. Thanks. – TonyStark4ever Jun 02 '17 at 23:07
-
-
See the answer I marked as correct. This is a more surgical edit that what I needed, but I think I will be using this for something else in my app (preventing buttons from rotating). Thanks for following up. – TonyStark4ever Jul 13 '17 at 05:03
I see two possibilities, and prefer the second because it doesn't rotate your views twice. I will use the term static view for the single view that you don't want to rotate (= the UIToolbar).
Possibility 1
Subscribe to device orientation changes. Then, when the orientation changes, and the UI rotates, rotate the static view in the opposite direction.
For example when you get a notification that the device is rotated 90° clockwise:
- The whole user interface (including the static view) will rotate 90° counterclockwise automatically
- Rotate the static view 90° clockwise, to cancel the automatic rotation behaviour.
Possibility 2
Disable the automatic rotation behaviour and then manually rotate all views except the one you don't want to rotate. To rotate multiple views at once: put them in a custom view and rotate that custom view. Here is a step-by-step guide:
Disable automatic rotation in your view controller. Do this by overriding the ViewController's shouldAutoRotate variable.
In interface builder: put everything except the static view in a custom UIView.
Observe device orientation changes and rotate the statusbar and your custom view (you created in step 2) when the device's orientation changes.
An example implementation in swift can be found at: GitHub/Dev1an/Static iOS toolbar
Rotating views
For the question "how to rotate views" have a look at: https://stackoverflow.com/a/28717635/2616297

- 4,427
- 1
- 22
- 33
Straightforward. You should simply override the shouldRotate
variable and set it to false for the VC you wish to prevent rotation in.
UPDATED - Swift 3
override var shouldAutorotate: Bool {
return false
}
I used the information from all of the answers here, as well as from this post overriding shouldAutorotate not working in Swift 3, to find the answer to my question. I created a MainNavigationController
class within my MainViewController.swift file and then overrided shouldAutorotate
like so:
import UIKit
class MainNavigationController: UINavigationController {
override var shouldAutorotate: Bool {
return false }
}
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
The last step was to go into my Main.storyboard file, click on the navigation controller for the view, and change its class to MainNavigationController
. Once I did that, it worked perfectly. Thanks to all the people who answered this question.

- 848
- 1
- 9
- 24