I'm working on a word game app that has as one of its view controllers a crossword grid. The app is an iPad app designed to be played in the middle of the table with the iPad flat on the table, so orientation is set in the app and viewControllers to be portrait only. Views are rotated for each user using transforms when it is their turn. Here is a sample from early development. You see the tiles in the center are rotated to face the blue player. The other players also each have a set of tiles oriented toward them.
If the user taps the grid icon, which is presented only at the start of the turn, the crossword screen is shown (here's a screen shot from another game):
If the user cannot play any letters, there is a Discard button that will allow the letters to be discarded. I'd like to add an alert, but the default behavior of the alert is to use the orientation of the device. Even though it's a portrait-only application, the interface gets rotated:
The consequence of that is since the rest of the app was designed to be used in portrait-only orientation, it is now rotated. I want to avoid that, but I'd also like the alert to be oriented toward the user. Important notes: since the iPad will be lying flat, the device won't actually be rotating between turns. Yet I still want the views to rotate.
I thought this could be achieved by setting the device or interface orientation before calling the alert (possible?), but I don't want the screen the alert is presented over to rotate, which is what's happening here when I place the simulator in landscape orientation. In actual play, the orientation would be whatever orientation was last in place when the iPad was set flat, so the view could rotate in any direction. A possible solution I thought of was to add the alert to a separate window. Can the second window have a different orientation? Can I set the orientation of the alertview directly?
Or maybe I should just write my own alertViewController. That may be the simplest solution.
Edit: I realized I didn't include this in the crossword VC, although it was in the calling VC:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var shouldAutorotate: Bool {
return false
}
Adding that prevented it from rotating, but now the alert shows in portrait, which is actually what I expected. Next I tried setting the transform of the alert programmatically:
alertViewController.view.transform = self.view.transform
But that didn't work.
I decided to write my own alert viewController. I did it using a XIB
file, with a couple labels and buttons as needed. I presented it as a freeform formsheet, providing a delegate variable for the calling viewController to use to be notified of the button action. Here's what it looks like.
I set the transform for the custom alert view controller's view to the transform of the calling view controller's view, i.e., the view controller that manages the crossword view.