【问题标题】:How to check landscape and portrait mode on appdelegate?如何在 appdelegate 上检查横向和纵向模式?
【发布时间】:2011-09-05 08:51:03
【问题描述】:

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

如何在 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 类型的对象上找不到属性接口方向。

我该怎么做?

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios4


    【解决方案1】:

    您应该检查您的 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
    }
    

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

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

    【讨论】:

    • 这在 appDelegate 中不起作用。就我而言,无论方向如何,条件始终是 LandscapeRight。
    【解决方案2】:
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    
    
        UIDeviceOrientation   orientation = [UIDevice currentDevice].orientation;
    
        NSString   *nibName = UIDeviceOrientationIsLandscape(orientation) ? @"Landscape" : @"Portrait";
    
    
        NSLog(@"orientation is %@",nibName);
    }
    

    【讨论】:

    • 我已经创建了一种方法 - appdelegate 上的 showplayer,因为我尝试检查横向和纵向方法,并且在每个其他页面上我调用 -showplayer 方法。根据你的建议,我认为无法给出每隔一页的笔尖名称,有没有其他方法可以查看风景肖像?
    • 设备方向如果平放可能会出现问题,您应该改用我认为的 status bas 方向
    【解决方案3】:
    -(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");
         }
      }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多