强制UIAlertView进入横向模式

时间:2012-02-06 12:47:58

标签: objective-c cocoa-touch uialertview cgaffinetransform

我需要在横向模式下显示UIAlertView。我尝试了显而易见的事情,在willPresentAlertView:委托方法中设置转换无效:

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

    alertView.transform = CGAffineTransformMakeRotation(M_PI_2);
}

有关如何解决此问题的任何建议吗?

5 个答案:

答案 0 :(得分:2)

您是否尝试过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];
}

答案 1 :(得分:1)

如果您使用UIViewController,它应自动轮播。 您是否忘记在YES中找到所需方向的shouldAutorotateToInterfaceOrientation

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

答案 2 :(得分:1)

显示警报的视图的方向是什么?我有同样的问题,我试图在横向视图中显示UIAlertView但总是以纵向显示。所以,我强制了状态栏的方向:

[[UIApplication sharedApplication] setStatusBarOrientation:theOrientation];

这对我有用。

答案 3 :(得分:1)

在出现警报窗口之前,将当前窗口显示设置在顶部。如果不这样做,您可以看到警报窗口旋转动画。

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

    [UIView setAnimationsEnabled:NO];

    self.view.window.windowLevel = 2003;

}

旋转警报窗口

-(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];

}

在“警报”窗口旋转后,将当前窗口移回。

-(void)showLandscapeAlertView {

    self.view.window.windowLevel = 0;

    [UIView setAnimationsEnabled:YES];

}

答案 4 :(得分:0)

我最近在同样的问题上苦苦挣扎。对我来说,解决方案是使用UIAlertController - 涵盖对UIAlertviewUIActionsheet的旧处理。

  1. 创建新的控制器,该控制器专用于UIAlertController,您必须重载方法viewWillAppearviewWillDisappear,如下例所示。
  2. <强> 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. 实现在需要的位置显示警报视图的方法。

      • (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];
        }