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.
Edit: here is a gist of the suggested answer, which I tried while it was being posted. The UI needs some tweaks, but you can see here that it works.
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.
Edit: here is a gist of the suggested answer, which I tried while it was being posted. The UI needs some tweaks, but you can see here that it works.
Share Improve this question edited 2 hours ago Victor Engel asked 5 hours ago Victor EngelVictor Engel 2,1352 gold badges26 silver badges50 bronze badges 3 |1 Answer
Reset to default 0Simply put, just don't use the built-in UIAlertViewController. It is merely a presented view controller, so you are free to present your own custom view controller whose view, if this is what you want, looks like a built-in alert, but isn't. You can achieve everything that UIAlertViewController does (the view in the middle of the screen, the darkening of everything behind it, the animation for appearing and disappering, even the parallax effect) with your own custom view controller.
In your case, the key difference would be that your custom view contents will have a transform applied so as to appear in a desired orientation — the very thing you cannot do with a built-in alert view.
setNeedsUpdateOfSupportedInterfaceOrientations
before presenting alertView – Cy-4AH Commented 4 hours ago