Fixed autorotate / resize support on iOS 6.0, tested with iPad and iPhone simulators with iOS 5.1 and 6.0
authorSam Lantinga <slouken@libsdl.org>
Sun, 23 Sep 2012 03:46:49 -0700
changeset 6451 52e5a05ca795
parent 6450 cf62de5b978f
child 6452 1895319021e4
Fixed autorotate / resize support on iOS 6.0, tested with iPad and iPhone simulators with iOS 5.1 and 6.0
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitviewcontroller.h
src/video/uikit/SDL_uikitviewcontroller.m
--- a/src/video/uikit/SDL_uikitopengles.m	Sun Sep 23 01:54:30 2012 -0700
+++ b/src/video/uikit/SDL_uikitopengles.m	Sun Sep 23 03:46:49 2012 -0700
@@ -131,9 +131,13 @@
         [view->viewcontroller setView:view];
         [view->viewcontroller retain];
     }
-
-    /* add the view to our window */
-    [uiwindow addSubview: view];
+    
+    // The view controller needs to be the root in order to control rotation on iOS 6.0
+    if (uiwindow.rootViewController == nil) {
+        uiwindow.rootViewController = view->viewcontroller;
+    } else {
+        [uiwindow addSubview: view];
+    }
 
     if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) {
         UIKit_GL_DeleteContext(_this, view);
--- a/src/video/uikit/SDL_uikitviewcontroller.h	Sun Sep 23 01:54:30 2012 -0700
+++ b/src/video/uikit/SDL_uikitviewcontroller.h	Sun Sep 23 03:46:49 2012 -0700
@@ -32,9 +32,8 @@
 
 - (id)initWithSDLWindow:(SDL_Window *)_window;
 - (void)loadView;
-- (void)statusBarFrameChanged:(NSNotification*)notification;
-- (void)onWindowSizeChanged;
+- (void)viewDidLayoutSubviews;
+- (NSUInteger)supportedInterfaceOrientations;
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient;
-- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
 
 @end
--- a/src/video/uikit/SDL_uikitviewcontroller.m	Sun Sep 23 01:54:30 2012 -0700
+++ b/src/video/uikit/SDL_uikitviewcontroller.m	Sun Sep 23 03:46:49 2012 -0700
@@ -44,9 +44,6 @@
     }
     self.window = _window;
 
-    // Register for notification when the status bar size changes    
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
-
     return self;
 }
 
@@ -55,12 +52,7 @@
     // do nothing.
 }
 
-- (void)statusBarFrameChanged:(NSNotification*)notification
-{
-    [self onWindowSizeChanged];
-}
-
-- (void)onWindowSizeChanged
+- (void)viewDidLayoutSubviews
 {
     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
         SDL_WindowData *data = self->window->driverdata;
@@ -68,22 +60,18 @@
         SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
         const CGSize size = data->view.bounds.size;
         int w, h;
-
+        
         w = (int)(size.width * displaymodedata->scale);
         h = (int)(size.height * displaymodedata->scale);
-
+        
         SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
     }
 }
 
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
+- (NSUInteger)supportedInterfaceOrientations
 {
-    // Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation
-    if (orient == UIInterfaceOrientationPortraitUpsideDown) {
-        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
-            return NO;
-    }
-
+    NSUInteger orientationMask = 0;
+    
     const char *orientationsCString;
     if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
         BOOL rotate = NO;
@@ -91,61 +79,46 @@
                                                             encoding:NSUTF8StringEncoding];
         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
-
-        switch (orient) {
-            case UIInterfaceOrientationLandscapeLeft:
-                rotate = [orientations containsObject:@"LandscapeLeft"];
-                break;
-
-            case UIInterfaceOrientationLandscapeRight:
-                rotate = [orientations containsObject:@"LandscapeRight"];
-                break;
-
-            case UIInterfaceOrientationPortrait:
-                rotate = [orientations containsObject:@"Portrait"];
-                break;
-
-            case UIInterfaceOrientationPortraitUpsideDown:
-                rotate = [orientations containsObject:@"PortraitUpsideDown"];
-                break;
-
-            default: break;
+        
+        if ([orientations containsObject:@"LandscapeLeft"]) {
+            orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
+        }
+        if ([orientations containsObject:@"LandscapeRight"]) {
+            orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
+        }
+        if ([orientations containsObject:@"Portrait"]) {
+            orientationMask |= UIInterfaceOrientationMaskPortrait;
         }
-
-        return rotate;
+        if ([orientations containsObject:@"PortraitUpsideDown"]) {
+            orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
+        }
+        
+    } else if (self->window->flags & SDL_WINDOW_RESIZABLE) {
+        orientationMask = UIInterfaceOrientationMaskAll;  // any orientation is okay.
+    } else {
+        if (self->window->w >= self->window->h) {
+            orientationMask |= UIInterfaceOrientationMaskLandscape;
+        }
+        if (self->window->h >= self->window->w) {
+            orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
+        }
     }
-
-    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
-        return YES;  // any orientation is okay.
+    
+    // Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation
+    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
+        orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
     }
-
-    // If not resizable, allow device to orient to other matching sizes
-    //  (that is, let the user turn the device upside down...same screen
-    //   dimensions, but it lets the user place the device where it's most
-    //   comfortable in relation to its physical buttons, headphone jack, etc).
-    switch (orient) {
-        case UIInterfaceOrientationLandscapeLeft:
-        case UIInterfaceOrientationLandscapeRight:
-            return (self->window->w >= self->window->h);
-
-        case UIInterfaceOrientationPortrait:
-        case UIInterfaceOrientationPortraitUpsideDown:
-            return (self->window->h >= self->window->w);
-
-        default: break;
-    }
-
-    return NO;  // Nothing else is acceptable.
+    return orientationMask;
 }
 
-// Send a resized event when the orientation changes.
-- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
 {
-    [self onWindowSizeChanged];
+    NSUInteger orientationMask = [self supportedInterfaceOrientations];
+    return (orientationMask & (1 << orient));
 }
 
+@end
+
 #endif /* SDL_VIDEO_DRIVER_UIKIT */
 
-@end
-
 /* vi: set ts=4 sw=4 expandtab: */