Forcing UIAlertView into landscape mode

后端 未结 5 1022
花落未央
花落未央 2021-01-07 14:49

I need to display a UIAlertView in landscape mode. I tried the obvious, setting the transform in the willPresentAlertView: delegate method to no avail:

相关标签:
5条回答
  • 2021-01-07 15:26

    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];
        }
        
    0 讨论(0)
  • 2021-01-07 15:33

    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];
    
    }
    
    0 讨论(0)
  • 2021-01-07 15:34

    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 */
    }
    
    0 讨论(0)
  • 2021-01-07 15:35

    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.

    0 讨论(0)
  • 2021-01-07 15:37

    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];
    }
    
    0 讨论(0)
提交回复
热议问题