Forcing UIAlertView into landscape mode

后端 未结 5 1049
花落未央
花落未央 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 
    
    @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];
        }
        

提交回复
热议问题