Ankit Chauhan
Ankit Chauhan

Reputation: 2405

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?

Upvotes: 2

Views: 8310

Answers (4)

Mohamed Jaleel Nazir
Mohamed Jaleel Nazir

Reputation: 5831

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  {
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
     {
       NSLog(@"Lanscapse");
     }
    if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
     {
       NSLog(@"UIDeviceOrientationPortrait");
     }
  }

Upvotes: 2

Parth Bhatt
Parth Bhatt

Reputation: 19469

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.

Upvotes: 7

B25Dec
B25Dec

Reputation: 2377

The delegate orientation is the orientation that we specify in the plist ,if we not specify in any orientation in plist it will take the portrait mode default .But in delegate you have only the key window that doesn't support the orientation you have to add the viewcontroller or any navigation controller with a view then you can call these function and they will give your the proper orientation for the device.

Upvotes: 1

- (void)applicationDidFinishLaunching:(UIApplication *)application {


    UIDeviceOrientation   orientation = [UIDevice currentDevice].orientation;

    NSString   *nibName = UIDeviceOrientationIsLandscape(orientation) ? @"Landscape" : @"Portrait";


    NSLog(@"orientation is %@",nibName);
}

Upvotes: 2

Related Questions