2015-07-21 8 views
5

Ho un'applicazione per iPad in cui creo un nuovo UIWindow all'inizio dell'applicazione e lo mostro mentre eseguo la sincronizzazione delle risorse. Quando l'app viene avviata mentre l'iPad è orientato in verticale, va tutto bene. Tuttavia, quando si utilizza l'orientamento orizzontale, la dimensione UIWindow's appena creata sembra soddisfacente, ma appare di lato e le coordinate sembrano tutte strane. Qui ci sono le screenshot per entrambi gli orientamenti verticale e orizzontale:UIWindow appena creato è di lato quando l'app viene aperta in orizzontale

enter image description here

enter image description here

Il paesaggio si è ottenuta ruotando a destra una volta.

Il pezzo di codice dove Creo e mostra UIWindow è il seguente:

UIWindow *window=[UIApplication sharedApplication].keyWindow; 
self.progressWindow = [[UIWindow alloc] initWithFrame:window.frame]; 
self.progressWindow.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f]; 
self.progressWindow.windowLevel = UIWindowLevelAlert; 

/* some other code to create the subviews */ 

[self.progressWindow makeKeyAndVisible]; 

Questo problema si verifica solo su IOS 8.

risposta

1

Per quanto ho capito, di recente ha creato UIWindow di non ruotare automaticamente quando l'orientamento cambia. Non so se questa è una novità per iOS 8 o se si tratta di un bug, ma sono stato in grado di superare il problema con il seguente pezzo di codice:

UIWindow *window=[UIApplication sharedApplication].keyWindow; 
self.progressWindow = [[UIWindow alloc] initWithFrame:window.frame]; 
self.progressWindow.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f]; 
self.progressWindow.windowLevel = UIWindowLevelAlert; 

/* some other code to create the subviews */ 

[self handleOrientationChange]; 
[self.progressWindow makeKeyAndVisible]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOrientationChange) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; 

Attuazione per handleOrientationChange è la seguente:

#define DegreesToRadians(degrees) (degrees * M_PI/180) 

- (void)handleOrientationChange 
{ 
    if (self.progressWindow) 
    { 
     // rotate the UIWindow manually 
     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
     [self.progressWindow setTransform:[self transformForOrientation:orientation]]; 

     // resize the UIWindow according to the new screen size 
     if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]) 
     { 
      // iOS 8 
      CGRect screenRect = [UIScreen mainScreen].nativeBounds; 
      CGRect progressWindowFrame = self.progressWindow.frame; 
      progressWindowFrame.origin.x = 0; 
      progressWindowFrame.origin.y = 0; 
      progressWindowFrame.size.width = screenRect.size.width/[UIScreen mainScreen].nativeScale; 
      progressWindowFrame.size.height = screenRect.size.height/[UIScreen mainScreen].nativeScale; 
      self.progressWindow.frame = progressWindowFrame; 
     } 
     else 
     { 
      // iOs 7 or below 
      CGRect screenRect = [UIScreen mainScreen].bounds; 
      CGRect progressWindowFrame = self.progressWindow.frame; 
      progressWindowFrame.origin.x = 0; 
      progressWindowFrame.origin.y = 0; 
      progressWindowFrame.size.width = screenRect.size.width; 
      progressWindowFrame.size.height = screenRect.size.height; 
      self.progressWindow.frame = progressWindowFrame; 
     } 
    } 
} 

- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation { 

    switch (orientation) { 

     case UIInterfaceOrientationLandscapeLeft: 
      return CGAffineTransformMakeRotation(-DegreesToRadians(90)); 

     case UIInterfaceOrientationLandscapeRight: 
      return CGAffineTransformMakeRotation(DegreesToRadians(90)); 

     case UIInterfaceOrientationPortraitUpsideDown: 
      return CGAffineTransformMakeRotation(DegreesToRadians(180)); 

     case UIInterfaceOrientationPortrait: 
     default: 
      return CGAffineTransformMakeRotation(DegreesToRadians(0)); 
    } 
} 

Ho ricevuto l'idea dalla risposta accettata della domanda this.

0

L'applicazione condivisa mantiene un riferimento alla finestra attraverso la sua proprietà Window a chiave:

let w = UIApplication.sharedApplication().keyWindow

Tale riferimento, tuttavia, è leggermente volatile, in quanto il sistema può creare finestre temporanee e le interpone come finestra chiave dell'applicazione.

cercano invece

[[UIApplication sharedApplication].delegate window] 
+0

Sto usando solo keyWindow per ottenere la sua cornice, quindi non penso che sia questo il problema. – halileohalilei

Problemi correlati