iOS: Device orientation on load

前端 未结 16 1904
遥遥无期
遥遥无期 2020-11-28 23:45

It seems that when my app loads, it does not know its current orientation:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (o         


        
16条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 00:20

    EDIT: I mis-read your question. This will allow you to start your application in certain orientations. Just realized you're trying to figure out the orientation on launch.

    There is a method to check the status bar orientation on UIApplication:

    [[UIApplication sharedApplication] statusBarOrientation];
    

    Original answer

    Try setting the application's accepted device orientations in the plist file:

    UISupportedInterfaceOrientations
    
        UIInterfaceOrientationPortrait
        UIInterfaceOrientationLandscapeLeft
        UIInterfaceOrientationLandscapeRight
    
    

    This will indicate that your application supports Portrait (home button at the bottom), landscape left, and landscape right.

    Then, in your UIViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) method to return YES when the app should rotate:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
         return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
    }
    

    This will tell the UIViewController to auto rotate if the device is in one of your supported orientations. If you wanted to support the upside-down orientation as well (portrait with home button on top) then add that to your plist and just return YES out of this method.

    Let us know how it works out.

提交回复
热议问题