2012-02-06 66 views
0

我需要在橫向模式下顯示UIAlertView。我嘗試了很明顯,設置在willPresentAlertView:委託方法變換無濟於事:強制UIAlertView進入橫向模式

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

    alertView.transform = CGAffineTransformMakeRotation(M_PI_2); 
} 

關於如何解決此問題有什麼建議?

回答

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]; 
} 
+0

這可能會在'willPresentAlertView'中工作,但我以前沒有嘗試過。 – 2012-02-06 13:13:52

+0

它可以在didPresentAlertView上工作:但它具有首先在錯誤位置顯示視圖的不良影響。 – cfischer 2012-02-06 13:26:31

+0

它在'willPresentAlertView'中工作嗎?如果可以的話,那可能會消除動畫效果。 – 2012-02-06 13:27:57

1

如果您使用的是UIViewController,它應該會自動旋轉。 忘了在shouldAutorotateToInterfaceOrientation中忘記將所需的方向還原YES

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

不,我返回YES,但它不自動旋轉。 :-( – cfischer 2012-02-06 13:58:08

+1

它應該,如果它在控制器的層次結構中很深,它們都應該啓用自動旋轉,並且在窗口級別有兩個或多個控制器可能會導致類似的問題 – djromero 2012-02-06 15:03:21

1

顯示警報的視圖的方向是什麼?我遇到了同樣的問題,我嘗試在橫向視圖中顯示UIAlertView,但始終以縱向顯示。所以,我強制的狀態欄的方向:

[[UIApplication sharedApplication] setStatusBarOrientation:theOrientation]; 

這對我有效。

0

最近我一直在努力解決相同的問題。對我而言,解決方案使用的是UIAlertController - 涵蓋舊版本的UIAlertviewUIActionsheet

  1. 創建新的控制器,這是從UIAlertController,在那裏你必須重載方法viewWillAppearviewWillDisappear,例如像在波紋管專用。

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

... 
  • 實現用於表示在你需要警報視圖方法。警報窗口出現

    • (無效){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]; 
      } 
      
  • 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]; 
    
    } 
    
    +0

    儘管此代碼可能有助於解決這個問題並沒有解釋_why_和/或_how_它回答了這個問題,提供這個額外的上下文將顯着提高它的長期教育價值,請編輯你的答案以增加解釋,包括什麼限制和假設適用。 – 2016-10-19 16:31:42

    +0

    作品像魅力! – SamuelChan 2017-06-23 08:06:45

    相關問題