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

空扰寡人 提交于 2019-12-03 17:01:52

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!