Detect iOS device orientation lock

前端 未结 3 689
青春惊慌失措
青春惊慌失措 2020-12-30 09:59

How can I programmatically check whether the device orientation is locked in iOS? I see nothing in UIDevice. I know that this is supposed to be transparent to the app. But I

3条回答
  •  攒了一身酷
    2020-12-30 10:45

    There's no way how to detect if orientation is locked or not. YouTube app doesn't lock to landscape, it just displays movie in landscape orientation, but when you rotate your iPhone, movie rotates as well (if there's no orientation lock).

    IOW orientation lock is handled by system, transparent to your application.

    If you want to achieve this functionality - just display your view in landscape mode even if the iPhone is in portrait mode and later enable rotation of your view. It will behave in the same way as YouTube app.

    Update for comment:

    .h
    
    BOOL rotationEnabled;
    
    .m
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
      return rotationEnabled || ( toInterfaceOrientation == UIInterfaceOrientationLandscapeRight );
    }
    
    - (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      rotationEnabled = YES;
    }
    

提交回复
热议问题