2015-07-21 7 views
5

Ich habe eine iPad-Anwendung, in der ich eine neue UIWindow am Anfang der App erstellen und es zeigen, während ich einige Ressourcen-Synchronisierung. Wenn die App gestartet wird, während sich das iPad im Hochformat befindet, ist alles in Ordnung. Allerdings scheint die neu erstellte UIWindow's Größe im Querformat gut zu sein, aber sie erscheint seitwärts und ihre Koordinaten scheinen alle merkwürdig. Hier sind die Screenshots für beide Hoch- und Querformat:Neu erstellte UIWindow ist seitwärts, wenn App auf Landschaft geöffnet ist

enter image description here

enter image description here

Die Landschaft ist man erhalten durch nach rechts einmal zu drehen.

Das Stück Code, wo ich erstellen und zeigen die UIWindow wie folgt:

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

Dieses Problem tritt nur auf iOS 8.

Antwort

1

Soweit ich verstanden, neu UIWindow die nicht automatisch erstellt drehen sich, wenn die Ausrichtung ändert. Ich weiß nicht, ob dies auf iOS neue 8 oder wenn es ein Fehler ist, aber ich war in der Lage, das Problem mit dem folgenden Code zu überwinden:

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

Implementierung für handleOrientationChange sich wie folgt:

#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)); 
    } 
} 

Ich habe die Idee von der angenommenen Antwort this Frage.

0

Die gemeinsame Anwendung einen Verweis auf das Fenster hält durch seine Tasten- Fenstereigenschaft:

let w = UIApplication.sharedApplication().keyWindow

Diese Referenz ist jedoch ist leicht flüchtig, weil das System temporäre Fenster erstellen und sie als das Schlüsselfenster der Anwendung zwischenschalten kann.

versuchen stattdessen

[[UIApplication sharedApplication].delegate window] 
+0

Ich benutze nur KeyWindow, um seinen Rahmen zu bekommen, also glaube ich nicht, dass das das Problem ist. – halileohalilei

Verwandte Themen