how to lock portrait orientation for only main view using swift

后端 未结 16 1907
野性不改
野性不改 2020-12-07 13:38

I have created an application for iPhone, using swift, that is composed from many views embedded in a navigation controller. I would like to lock the main v

16条回答
  •  日久生厌
    2020-12-07 14:09

    In the main controller where you want portrait,

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
    self.orientation = UIInterfaceOrientationMaskPortrait;
    //Or self.orientation = UIInterfaceOrientationPortraitUpsideDown
    }
    

    and in subVC where you want Landscape use

      self.orientation =  UIInterfaceOrientationLandscapeLeft:
       self.orientation =  UIInterfaceOrientationLandscapeRight:
    

    or you can override this method

    - (NSUInteger)supportedInterfaceOrientations
    {
      return UIInterfaceOrientationMaskPortrait;
    } 
    

    This is how i would do it with Obj-c in iOS7, i think this code would work in iOS8 too

提交回复
热议问题