Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rotate UIAlertController (and Alert) in landscape orientation?

I am editing an app where I added a new UIViewController to configure a multiplayer game in one layout.

I have added two buttons for each player (top button, bot button). Each button generates an alert at the screen (remember it is in landscape orientation ) but the problem is, player 2 do not see the alert rotated, I mean, the alert is supposed to be shown in his orientation, in front of me.

As there any way to do this? I want to rotate an Alert for the player 2 will not see the information of the alert turned.

Here the alert code I have:

@IBAction func ShowAlert(){
let message = "Test"
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)

let action = UIAlertAction(title: "OK", style: .Default, handler: nil)

    alert.addAction(action)

    presentViewController(alert, animated: true, completion: nil)
}
like image 789
lexiesk Avatar asked Dec 14 '22 11:12

lexiesk


2 Answers

Here is a way to rotate an alert controller view, although it is not perfect. The controller tries to present based on the orientation and I found that attempting to transform it before it is presented is unsuccessful.

In order to not see "incorrect" orientation initially, you must set the alert to hidden and then make it visible once it is "presented".

let alert // setup

alert.view.hidden = true;
[self presentViewController:c animated:YES completion:^{
    alert.view.transform = CGAffineTransformMakeRotation(M_PI);
    alert.view.hidden = false;
}];

The only outstanding issue I have with this is that after the user interacts with the alert, it flashes back to the initial transformation (orientation dependent). I currently have not found a solution for this, but will update if I ever do.


Alternatively, using a custom "alert" would solve this problem as you would have more control over its view. This is probably the more reliable approach.

like image 124
Firo Avatar answered Apr 27 '23 08:04

Firo


Here is my solution. presentViewController with no animation to avoid flash when present the alert controller.

[self presentViewController:alert animated:NO completion:^{
    [alertController.view setTransform: CGAffineTransformMakeRotation(M_PI_2)];
 }];

then create a UIAlertController category named e.g. UIAlertController+Orientation, hide the view in viewWillDisappear to avoid flash back when dismiss the alert controller.

#import "UIAlertController+Orientation.h"

@implementation UIAlertController (Orientation)

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view setHidden:YES];
}

- (BOOL) shouldAutorotate {
    return NO;
}

@end
like image 33
宋申易 Avatar answered Apr 27 '23 07:04

宋申易