如何在appdelegate上查看横向和纵向模式?

时间:2011-09-05 08:51:04

标签: iphone objective-c cocoa-touch ios4

我想以横向和纵向模式运行应用程序,

如何在Appdelegate上查看横向和纵向模式?

我试着打电话

- (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));

}

但是给出了错误 在appdelegate类型的对象上找不到属性界面方向。

我该怎么做?

4 个答案:

答案 0 :(得分:7)

您应该使用以下方法检查iPad的当前方向。我建议你在每个视图上检查这些条件,并按照当前的方向行事:

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
}

我希望这会对你有所帮助。 :)

如果您需要任何帮助,请随时与我联系。

答案 1 :(得分:2)

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


    UIDeviceOrientation   orientation = [UIDevice currentDevice].orientation;

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


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

答案 2 :(得分:2)

-(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");
     }
  }

答案 3 :(得分:1)

委托方向是我们在plist中指定的方向,如果我们没有在plist中指定任何方向,它将采用纵向模式默认。但是在委托中你只有不支持方向的关键窗口你必须添加viewcontroller或任何带有视图的导航控制器,然后你可以调用这些函数,它们将为你的设备提供正确的方向。