简体   繁体   English

ViewDidLoad中的iPhone设备当前方向

[英]iphone device current orientation in ViewDidLoad

Please, help me.请帮我。 I have methods:我有方法:

-(void) getCurrentOrientation{

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

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

but when i call this getCurrentOrientation throw viewDidLoad但是当我调用这个 getCurrentOrientation 时抛出 viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self getCurrentOrientation];

}

NSLog is empty. NSLog 为空。 Whats wrong ?怎么了 ? I also try我也试试

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

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

    {
        NSLog(@"LandscapeRight"); 
    }

}

but that varint also empty.但那个 varint 也是空的。

I need to know in which ORIENTATION user launch APP!我需要知道用户在哪个 ORIENTATION 启动应用程序!

Please, give me any advice.请给我任何建议。

Your code is absolutely correct and there is no problem.你的代码是绝对正确的,没有问题。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

This statement only works in original devices.此声明仅适用于原始设备。

However you want to check in simulator you can check as但是你想检查模拟器,你可以检查

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

Update:更新:

I have tested for you in both device and simulator.我已经在设备和模拟器中为您进行了测试。 Initially it would show portrait only in viewDidLoad though you will keep device in landscape.最初它只会在 viewDidLoad 中显示纵向,尽管您将设备保持横向。 First Controller will look into shouldAutoRotate method.第一个控制器将研究 shouldAutoRotate 方法。

You should not depend on viewDidLoad for initial orientation.您不应依赖viewDidLoad进行初始方向。 You should depend on shouldAutorotate method initially for exact orientation.您应该最初依赖 shouldAutorotate 方法来获得准确的方向。

When the app first loads, UIDevice.current.orientation isn't valid.当应用首次加载时, UIDevice.current.orientation 无效。 But UIApplication.shared.statusBarOrientation is.但是 UIApplication.shared.statusBarOrientation 是。 UIDevice.current.orientation is the better way to check orientation, in general, though.不过,一般来说,UIDevice.current.orientation 是检查方向的更好方法。 So, this method will handle all situations所以,这个方法将处理所有情况

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

Try replace your shouldAutorotateMethod by below shouldAutorotate尝试用下面的 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)
  }

This may help you out.这可能会帮助你。

If you want to just check the orientation of app then use following code:如果您只想检查应用程序的方向,请使用以下代码:

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

or或者

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

in swift 3 or swift 4 .在 swift 3 或 swift 4 中。 you can use this code in viewDidLoad()..您可以在 viewDidLoad() 中使用此代码。

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

Is your viewDidLoad called?你的viewDidLoad被调用了吗? Have you put a breakpoint in there?你有没有在那里设置断点?

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

The documentation says, that the orientation always returns 0, if you didn't call beginGeneratingDeviceOrientationNotifications .文档说,如果您没有调用beginGeneratingDeviceOrientationNotifications ,则方向始终返回 0。 Maybe calling it just before you try to get the orientation is not enough.也许在您尝试获取方向之前调用它是不够的。 Try to move the call to application:didFinishLaunchingWithOptions: .尝试将调用移动到application:didFinishLaunchingWithOptions:

However, the best thing is using the orientation supplied by the controller - [UIViewController interfaceOrientation] and the parameter passed to shouldAutorotateToInterfaceOrientation .但是,最好的方法是使用控制器提供的方向 - [UIViewController interfaceOrientation]和传递给shouldAutorotateToInterfaceOrientation的参数。

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

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