Search code examples
iphoneobjective-ccocoa-touchios4

How to check landscape and portrait mode on appdelegate?


i want to run app in landscape and portrait mode,

How to check landscape and portrait mode on Appdelegate?

I try to call

- (BOOL) isPad{ 
#ifdef UI_USER_INTERFACE_IDIOM
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#else
    return NO;
#endif
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([self isPad]) {
        NSLog(@"is pad method call");
        return YES;
    }
    else 
    {
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);
    }

}



- (BOOL)isPadPortrait
{

    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationPortrait
                || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}


- (BOOL)isPadLandscape
{

    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
                || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));

}

but gives error Property interface orientation not found on object of type appdelegate.

How can i do that?


Solution

  • You should check the current orientation of your iPad using. I would recommend you to check these conditions on every view and act according to the current orientation:

    if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
    {
          //Do what you want in Landscape Left
    }
    else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
    {
          //Do what you want in Landscape Right
    }
    else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
    {
         //Do what you want in Portrait
    }
    else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
          //Do what you want in Portrait Upside Down
    }
    

    I hope this helps you. :)

    Feel free to contact me if you require any help.