Forcing UIAlertView into landscape mode

左心房为你撑大大i 提交于 2019-12-01 02:24:12

Have you tried in the didPresentAlertView?

- (void)didPresentAlertView:(UIAlertView *)alertView
{
    // UIAlertView in landscape mode
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.1];
    alertView.transform = CGAffineTransformRotate(alertView.transform, 3.14159/2);
    [UIView commitAnimations];
}

It should rotate automatically if you're using UIViewController. Did you forget to return YES for the desired orientations in shouldAutorotateToInterfaceOrientation?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES; /* auto rotate always */
}

What is the orientation of the view that shows the alert? I had the same problem, I tried to show an UIAlertView inside a landscape view but always appeared in portrait orientation. So, I forced the orientation of the status bar:

[[UIApplication sharedApplication] setStatusBarOrientation:theOrientation];

That worked to me.

imthunder

Before the Alert Window appeared , Set the current window display at the top. If not do this ,you can see the alert window's rotate animate.

-(void) willPresentAlertView:(UIAlertView *)alertView {

    [UIView setAnimationsEnabled:NO];

    self.view.window.windowLevel = 2003;

}

Rotate the Alert Window

-(void)didPresentAlertView:(UIAlertView *)alertView
{

    UIWindow * alertWindow = alertView.window;
    alertWindow.transform = CGAffineTransformMakeRotation(M_PI / 2);
    alertWindow.bounds = CGRectMake(0, 0, SCREEN_HEIGHT,SCREEN_WIDTH);
    alertWindow.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showLandscapeAlertView) userInfo:nil repeats:NO];

}

After the Alert window rotated, move the current window back.

-(void)showLandscapeAlertView {

    self.view.window.windowLevel = 0;

    [UIView setAnimationsEnabled:YES];

}
ivo

I was struggling with quite the same issue lately. For me solution was using UIAlertController - cover older handling of UIAlertview and UIActionsheet.

  1. Create new controller, which is dedicated from UIAlertController, where you must overloaded methods viewWillAppear and viewWillDisappear, like in example bellow.

AlertViewController.h

#import <UIKit/UIKit.h>

@interface AlertViewController : UIAlertController 

@end

AlertViewController.m

#import "AlertViewController.h"

@interface AlertViewController ()

@end

@implementation AlertViewController

- (void) viewWillAppear:(BOOL)animated {
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI_2)];
}

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

...
  1. implement method for showing alert view where you needed.

    • (void) showInfoAlertView {

          AlertViewController *alert = [AlertViewController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert];
      
          UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
          [alert addAction:ok];
          [self presentViewController:alert animated:NO completion:nil];
      }
      
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!