简体   繁体   中英

iphone device current orientation in ViewDidLoad

Please, help me. I have methods:

-(void) getCurrentOrientation{

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

    if(orientation == UIInterfaceOrientationPortrait){
        NSLog(@"portrait");
    } else if(orientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"LandscapeRight");
    } else if(orientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"LandscapeLeft");
    }   
}

but when i call this getCurrentOrientation throw viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self getCurrentOrientation];

}

NSLog is empty. Whats wrong ? I also try

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)) {
        NSLog(@"portrait");

    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 

    {
        NSLog(@"LandscapeRight"); 
    }

}

but that varint also empty.

I need to know in which ORIENTATION user launch APP!

Please, give me any advice.

Your code is absolutely correct and there is no problem.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

This statement only works in original devices.

However you want to check in simulator you can check as

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
    // portrait             
} else {
    // landscape
}

Update:

I have tested for you in both device and simulator. Initially it would show portrait only in viewDidLoad though you will keep device in landscape. First Controller will look into shouldAutoRotate method.

You should not depend on viewDidLoad for initial orientation. You should depend on shouldAutorotate method initially for exact orientation.

When the app first loads, UIDevice.current.orientation isn't valid. But UIApplication.shared.statusBarOrientation is. UIDevice.current.orientation is the better way to check orientation, in general, though. So, this method will handle all situations

var isLandscape: Bool {
    return UIDevice.current.orientation.isValidInterfaceOrientation 
        ? UIDevice.current.orientation.isLandscape 
        : UIApplication.shared.statusBarOrientation.isLandscape
}

Try replace your shouldAutorotateMethod by below shouldAutorotate

  - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
  {
       if(interfaceOrientation == UIInterfaceOrientationPortrait){
          NSLog(@"portrait");    
       } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) {       
          NSLog(@"LandscapeRight");        
       } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {      
          NSLog(@"LandscapeLeft"); 
       }   
       return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown)
  }

This may help you out.

If you want to just check the orientation of app then use following code:

- (void) viewDidLoad {
    [super viewDidLoad];
    BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
    // now do whatever you need
}

or

-(void)viewDidLoad
{
    if  (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        //landscape view code
    } 
    else
    {
         //portrait view code
    }
}

in swift 3 or swift 4 . you can use this code in viewDidLoad()..

let orientation = UIApplication.shared.statusBarOrientation
 if orientation == .portrait {
        // portrait   
 } else if orientation == .landscapeRight || orientation == 
.landscapeLeft{
         // landscape     
 }

Is your viewDidLoad called? Have you put a breakpoint in there?

What about just printing NSLog(@"%i", [[UIDevice currentDevice] orientation]) ?

The documentation says, that the orientation always returns 0, if you didn't call beginGeneratingDeviceOrientationNotifications . Maybe calling it just before you try to get the orientation is not enough. Try to move the call to application:didFinishLaunchingWithOptions: .

However, the best thing is using the orientation supplied by the controller - [UIViewController interfaceOrientation] and the parameter passed to shouldAutorotateToInterfaceOrientation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM