How to constrain autorotation to a single orientation for some views, while allowing all orientations on others?

后端 未结 5 1325
孤街浪徒
孤街浪徒 2020-12-01 03:44

This question is about iOS device rotation and multiple controlled views in a UINavigationController. Some views should be constrained to portrait orientation, and

5条回答
  •  甜味超标
    2020-12-01 04:29

    There was a similar question a few years ago with a number of answers. Here is a recent answer from someone to that question:
    Is there a documented way to set the iPhone orientation?

    From what I understand, this is a problem a lot of people have and mostly hacks are the only way to fix it. Look through that thread if you haven't seen it before and see if anything works for you.

    On a side note, I had a similar problem a while back when I was calling something in shouldAutorotate and I added some code to viewWillAppear to try to fix it. I honestly can't remember if it worked and I don't have a Mac anymore to try it out, but I found the code and I'll paste it here in case it gives any inspiration.

    - (void)viewWillAppear:(BOOL)animated{
      UIInterfaceOrientation o;
      switch ([UIDevice currentDevice].orientation) {
        case UIDeviceOrientationPortrait:
            o = UIInterfaceOrientationPortrait;
            break;
        case UIDeviceOrientationLandscapeLeft:
            o = UIInterfaceOrientationLandscapeLeft;
            break;
        case UIDeviceOrientationLandscapeRight:
            o = UIInterfaceOrientationLandscapeRight;
            break;
        default:
            break;
      }
    
      [self shouldAutorotateToInterfaceOrientation:o];
    }
    

提交回复
热议问题