coder
coder

Reputation: 5390

after launch in landscape cannot force view to start in portrait

I know that this subject has been asked before, but I went over all solutions and none solved the case I'm having.

My app is all in portrait format, only one view controller in landscape format.

I added to all view controllers the following code to force Portrait Orientation regardless the device orientation:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       return UIInterfaceOrientationMaskPortrait;
 }

 -(BOOL)shouldAutorotate{
      return NO;
 }
 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
 {
      return UIInterfaceOrientationMaskPortrait;
 }

 -(void)ViewDidLoad()
 {
      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
 }

If the device is set in portrait mode the viewController opens correctly, but if the device is set landscape mode before opening the app, then the app launches the splash screen in landscape and opens the view controller in landscape with part of the view clipped, then it rotates it to portrait mode, and keeps part of the screen clipped, as shown in the picture.

enter image description here

in the app delegate:

 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
 {
      return UIInterfaceOrientationMaskPortrait;;
 }

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
 {
      return UIInterfaceOrientationMaskAllButUpsideDown;
 }

And in the appDelegate also, just before adding the view controller to the navigation controller I added the following:

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];

Upvotes: 1

Views: 687

Answers (4)

Kimi Chiu
Kimi Chiu

Reputation: 2173

I'm having the same problem. But I fix it by rotate the device orientation programmatically.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if UIDevice.current.orientation != .portrait {        
        UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")
    }

    return .portrait
}

Upvotes: 1

alasker
alasker

Reputation: 331

You need to set your project as Portrait only to make sure the app launches in portrait regardless of the device's orientation.

Also, make sure you have supportedInterfaceOrientationsForWindow implemented on your AppDelegate and that it returns UIInterfaceOrientationMaskAll or whatever mask makes sense for your project.

With all that, you'll have the app launching with portrait and control over allowed orientations per view controller, with the implementation you already have.

Upvotes: 0

coder
coder

Reputation: 5390

ok here what I did and it finally working well, hope it can help someone:

in appDelegate I'm putting the following:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

To introduce the main view I'm using the following from appDelegate:

self.navigationController = [[UINavigationControllerSub alloc] initWithRootViewController:libraryVC];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

portraitSize = CGSizeMake(navigationController.view.frame.size.width, navigationController.view.frame.size.height);

[self.window setRootViewController:navigationController]; 

In the main view which I launched from the app delegate and I need it to be only portrait I'm doing the following:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [[UIApplication sharedApplication]     setStatusBarOrientation:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate setShouldRotate:NO];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}

When I want to launch a view where I need it to be landscape I use the following just before launching:

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES];
[UIApplication sharedApplication].statusBarOrientation = UIDeviceOrientationLandscapeLeft;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];

Upvotes: 0

adamfowlerphoto
adamfowlerphoto

Reputation: 2751

This seems to be a new thing since iOS 11. I fixed it by allowing autorotate but only supporting portrait.

(BOOL)shouldAutorotate{
      return YES;
}
(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
     return UIInterfaceOrientationMaskPortrait;
}

Upvotes: 1

Related Questions