I am updating an iOS 6 app to iOS 8 (iPad), and any new UIWindows
that I create are always showing up in Portrait mode. The app originally supported both the Portrait and Landscape orientations but now will only support Landscape.
I've changed the supported orientations in the project file to Landscape Left and Landscape Right. The entire UI shows up in landscape, as expected, but when I create a new UIWindow
, it shows up in portrait. The new UIWindow
's frame matches the screen's frame exactly, so I can't imagine why/how it is showing up in portrait mode.
The following code is what I am using to create and show the new UIWindow
, which acts as a modal:
var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds)
modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)
modalWindow.addSubview(customView)
modalWindow.makeKeyAndVisible()
I've been struggling with this for a few hours; shouldn't the UIWindow
be in landscape mode by default since the app only supports the Landscape orientation?
I would greatly appreciate any advice on how to resolve this issue.
EDIT:
I just created a new test app that only supports Landscape Left and Landscape Right, and the issue occurs there as well. Is this a bug? I can't seem to understand why the UIWindow would think the app is in Portrait mode when it's in Landscape mode.
EDIT (July 2, 2015)
This answer breaks in iOS 8.3+ and possibly previous versions of iOS 8. I DO NOT recommend that anyone uses it, especially since it is not guaranteed to work in future iOS versions.
My new solution uses the standard
presentViewController
method of aUIViewController
. I initialize aUIViewController
, add my custom modalView
as a subview of theUIViewController
, and then position the modalView
using constraints. Works like a charm!Original Answer
I left this for a while but decided to come back to it today. I ended up rotating the modal window -90 degrees since it is automatically rotated 90 degrees to be displayed in portrait. I also added a small calculation for the modal window's width and height to support both iOS 7 and iOS 8. Here is my new code:
As stated, this works great on both iOS 7+ and iOS 8+! When working with subviews, note that the modal's
width
andheight
values are switched since it's displaying in portrait. So, if you want to center a subview horizontally, use the modal window'sheight
and the subview'swidth
instead of both views'width
.