【问题标题】:Device orientation not work properly in iOS设备方向在 iOS 中无法正常工作
【发布时间】:2016-05-04 08:31:57
【问题描述】:
+(CGFloat)maxTextWidth
{
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
    {
        if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
           {
        return 220.0f;
           }
           else
           {
               return 400.0f;
           }
    }
    else
    {
        if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        {
        return 400.0f;
        }
    else
    {
        return 700.0f;
    }
    }
}

当我的设备按照上述方法处于纵向时,它会返回纵向值,但问题是在纵向时有时它会返回纵向值,有时它会返回横向值,但我的设备始终处于纵向。

【问题讨论】:

    标签: ios objective-c xcode7 orientation landscape-portrait


    【解决方案1】:

    在你的 if 语句中检查状态栏方向而不是设备方向..

    +(CGFloat)maxTextWidth
    {
        if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
        {
            if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
               {
            return 220.0f;
               }
               else
               {
                   return 400.0f;
               }
        }
        else
        {
            if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
            {
            return 400.0f;
            }
        else
        {
            return 700.0f;
        }
        }
    }
    

    【讨论】:

    • 帮助我,作为 iOS 11 的 UIDeviceOrientationIsPortrait 问题。
    【解决方案2】:

    将此代码复制到您的 viewdidload 中。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didrotatedevice:) name:UIDeviceOrientationDidChangeNotification object:nil];
    

    并将此方法复制到您的课程中

    - (void)didrotatedevice:(NSNotification *)notification
    {
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    
    
        if (orientation == UIDeviceOrientationLandscapeLeft)
        {
              NSLog(@"Landscape Left!");
    
        }
        else if (orientation == UIDeviceOrientationLandscapeRight)
        {
             NSLog(@"Landscape Right!");
    
        }
        else
        {
              NSLog(@"Potrait!");
    
        }
      }
    

    如果您有任何疑问,请告诉我?

    【讨论】:

      【解决方案3】:

      你为什么不使用

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      

      或者你可以使用这个

      -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      

      或者这个

      -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
      

      这些方法检测任何方向变化。 在这些方法中设置您的 UI 更改。

      【讨论】:

      • 我在聊天,以便根据设备方向返回最大文本宽度,以便我使用类方法,上面的代码与您的代码相同......
      猜你喜欢
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      相关资源
      最近更新 更多