1

I'm currently building an iOS app (supporting iOS >= 8) that has initial splash screen animation on start up. This animation blocks the entire screen during 2~3 seconds and then hides itself.

Goal is that the screen orientation should be fixed to portrait mode, and be released when the animation is over. This can be easily achieved by the following code:

- (void)onSplashAnimationEnded {
    _isFinished = YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (_isFinished) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

The problem is that when a user launched the app in landscape mode, and when the animation is finished, the device orientation (currently in landscape mode) does not apply to the app (locked in portrait mode, as intended).

It seems natural that the current orientation of device is automatically applied to the app right after the animation is over. I tried the followings to solve this and none of them works.

- (void)onSplashAnimationEnded {
    _isFinished = YES;

    // #1
    [UIViewController attemptRotationToDeviceOrientation];

    // #2
    [[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];

    // #3
    [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidChangeStatusBarOrientationNotification object:[UIApplication sharedApplication]];
}

I really hope someone to share any workarounds for this issue.

Edit #1

I have read the other question that is linked to mine, and want to add some detail about it.

It is possible to lock or unlock device rotation in a certain situation as described in the link, and I already implemented the feature in other way.

What is left can be regenerated by the following procedure:

  1. Set iPad in landscape mode.
  2. Launch the app (which will force the orientation in portrait mode), without rotating the device.
  3. When the animation is over, the device remains in portrait mode (which is odd because the device is in landscape mode).
  4. Orientation can be fixed when user tries to rotate to portrait > landscape mode one or twice.
  5. What I do want to do is to eliminate the step #4.
Tack-Gyu Lee
  • 448
  • 3
  • 11
  • you have to display only launch screen in landscape mode? – Birendra Mar 31 '17 at 08:07
  • why do you need to fix the app's loading screen at portrait mode? How about 2 loading screens for portrait and landscape mode? – JackyW Mar 31 '17 at 08:32
  • @JackyW I was told that there's difficulty in providing splash screen images for both portrait and landscape mode, so that's why I keep the initial orientation mode as portrait only. – Tack-Gyu Lee Mar 31 '17 at 09:21
  • @Birendra Nope, the whole features is accessible in both portrait and landscape mode, only the initial splash screen animation should be seen in portrait mode. – Tack-Gyu Lee Mar 31 '17 at 09:22
  • try this (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if ([self.window.rootViewController.presentedViewController isKindOfClass:[MediaViewController class]]) { MediaViewController *second = (MediaViewController *) self.window.rootViewController.presentedViewController; if (second.isPresented) { return UIInterfaceOrientationMaskAll; } else return UIInterfaceOrientationMaskPortrait; } else return UIInterfaceOrientationMaskPortrait; } – Birendra Mar 31 '17 at 09:36
  • Possible duplicate of [How to force or disable interface orientation for some but not all UIViewController?](http://stackoverflow.com/questions/31758706/how-to-force-or-disable-interface-orientation-for-some-but-not-all-uiviewcontrol) – Anand Suthar Mar 31 '17 at 10:37
  • @Birendra Sorry, that makes device rotation locked or unlocked for different situation (as I already did in my code snippet), but doesn't solve the problem of "invoking device rotation when needed". I will further explain my situation on my question. – Tack-Gyu Lee Apr 03 '17 at 00:32
  • @Birendra Actually, your code snippet perfectly works with a little adjustment. I posted an answer with details. Thanks anyway. – Tack-Gyu Lee Apr 03 '17 at 04:43

1 Answers1

0

After all, it is possible to implement the feature I wanted by the following combination, without any other settings in other view controllers:

// Control device orientation only in AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return _isFinished ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait;
}

- (void)onSplashAnimationEnded {
    _isFinished = YES;

    // Invoke OS orientation change message
    [UIViewController attemptRotationToDeviceOrientation];
}

For this, device orientation should be available in other view controllers.

Tack-Gyu Lee
  • 448
  • 3
  • 11