繁体   English   中英

ViewDidLoad中的iPhone设备当前方向

[英]iphone device current orientation in ViewDidLoad

请帮我。 我有方法:

-(void) getCurrentOrientation{

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

    if(orientation == UIInterfaceOrientationPortrait){
        NSLog(@"portrait");
    } else if(orientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"LandscapeRight");
    } else if(orientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"LandscapeLeft");
    }   
}

但是当我调用这个 getCurrentOrientation 时抛出 viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self getCurrentOrientation];

}

NSLog 为空。 怎么了 ? 我也试试

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)) {
        NSLog(@"portrait");

    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 

    {
        NSLog(@"LandscapeRight"); 
    }

}

但那个 varint 也是空的。

我需要知道用户在哪个 ORIENTATION 启动应用程序!

请给我任何建议。

你的代码是绝对正确的,没有问题。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

此声明仅适用于原始设备。

但是你想检查模拟器,你可以检查

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
    // portrait             
} else {
    // landscape
}

更新:

我已经在设备和模拟器中为您进行了测试。 最初它只会在 viewDidLoad 中显示纵向,尽管您将设备保持横向。 第一个控制器将研究 shouldAutoRotate 方法。

您不应依赖viewDidLoad进行初始方向。 您应该最初依赖 shouldAutorotate 方法来获得准确的方向。

当应用首次加载时, UIDevice.current.orientation 无效。 但是 UIApplication.shared.statusBarOrientation 是。 不过,一般来说,UIDevice.current.orientation 是检查方向的更好方法。 所以,这个方法将处理所有情况

var isLandscape: Bool {
    return UIDevice.current.orientation.isValidInterfaceOrientation 
        ? UIDevice.current.orientation.isLandscape 
        : UIApplication.shared.statusBarOrientation.isLandscape
}

尝试用下面的 shouldAutorotate 替换你的 shouldAutorotateMethod

  - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
  {
       if(interfaceOrientation == UIInterfaceOrientationPortrait){
          NSLog(@"portrait");    
       } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) {       
          NSLog(@"LandscapeRight");        
       } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {      
          NSLog(@"LandscapeLeft"); 
       }   
       return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown)
  }

这可能会帮助你。

如果您只想检查应用程序的方向,请使用以下代码:

- (void) viewDidLoad {
    [super viewDidLoad];
    BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
    // now do whatever you need
}

或者

-(void)viewDidLoad
{
    if  (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        //landscape view code
    } 
    else
    {
         //portrait view code
    }
}

在 swift 3 或 swift 4 中。 您可以在 viewDidLoad() 中使用此代码。

let orientation = UIApplication.shared.statusBarOrientation
 if orientation == .portrait {
        // portrait   
 } else if orientation == .landscapeRight || orientation == 
.landscapeLeft{
         // landscape     
 }

你的viewDidLoad被调用了吗? 你有没有在那里设置断点?

只打印NSLog(@"%i", [[UIDevice currentDevice] orientation])怎么样?

文档说,如果您没有调用beginGeneratingDeviceOrientationNotifications ,则方向始终返回 0。 也许在您尝试获取方向之前调用它是不够的。 尝试将调用移动到application:didFinishLaunchingWithOptions:

但是,最好的方法是使用控制器提供的方向 - [UIViewController interfaceOrientation]和传递给shouldAutorotateToInterfaceOrientation的参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM