Get launch orientation of iPad app

后端 未结 9 703
无人及你
无人及你 2020-12-02 16:19

In my iPad app, I need to run some layout code to set the proper layout depending on the orientation. By default, the layout is configured for the landscape orientation, so

9条回答
  •  被撕碎了的回忆
    2020-12-02 16:38

    It's not true that you can't figure out the launch orientation, it is true that it's a pain in the rear to do so.

    here's what you need to do.

    your first UIViewController needs to have some special logic to nab the information you'd like.
    you might even want to create a UIStartupController just for these purposes if it's that important to your flow.
    in the case of my project, we already had such a startup controller present.

    all you need is the following code

    -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
        self.launchOrientation = UIDeviceOrientationUnknown;
      }
      return self;
    }
    
    -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                    duration:(NSTimeInterval)duration
    {
      [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
      if (self.launchOrientation == UIDeviceOrientationUnknown && duration > 0)
        self.launchOrientation = UIInterfaceOrientationPortrait;
      else
        self.launchOrientation = toInterfaceOrientation;
    }
    

    basically, if we're not launching in UIInterfaceOrientationPortrait, the first rotation callback sequence will actually reveal the launch orientation.
    if launched in UIInterfaceOrientationPortrait, then we need to check that the first rotation's duration is non zero, and then we know that it was launched from portrait.

提交回复
热议问题