How can I disable landscape orientation?

2019-01-11 01:13发布

I am making an iPhone app and I need it to be in portrait mode, so if the user moves the device sideways, it does not automatically rotate. How can I do this?

6条回答
做自己的国王
2楼-- · 2019-01-11 01:25

Swift 3 If you have a navigationController, subclass it like this (for portrait only):

class CustomNavigationViewController: UINavigationController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
  }

  override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIInterfaceOrientation.portrait
  }
}
查看更多
够拽才男人
3楼-- · 2019-01-11 01:29

Xcode 5 and above

  • Click your project in the Project Navigator in the left sidebar to open the project settings
  • Go to the General tab.
  • Uncheck the options you don't want in the Deployment Info section, under Device Orientation

screenshot showing where to click

查看更多
够拽才男人
4楼-- · 2019-01-11 01:33

Xcode 4 and below

For those who missed it: you can use the project settings screen to fix orientations throughout the app (no need to override methods in every controller):

enter image description here

It's as simple as toggling the supported interface orientations. You can find by clicking on your Project in the left panel > the app target > Summary tab.

查看更多
虎瘦雄心在
5楼-- · 2019-01-11 01:33

If you want to disable landscape orientation for both iPhone and iPad.

Go to Targets and Go to the General tab. See the below screen and deselect landscape left and landscape right.

enter image description here

Here in this case only iPhone landscape mode will be disabled not for iPad. For iPad all modes are anabled. If you want select device option from Universal to iPad. It will looks like this. See below screen.

enter image description here

Now you need to deselect all modes except Portrait for iPad. See below screenshot.

enter image description here

Now you successfully disabled all modes except Portrait for all devices.

查看更多
虎瘦雄心在
6楼-- · 2019-01-11 01:33

Removing the method shouldAutorotateToInterfaceOrientation from your class entirely also works. If you don't plan on rotating then it makes no sense to have the method in your class, the less code the better, keeps things clean.

查看更多
叛逆
7楼-- · 2019-01-11 01:41

To disable orientations for a particular View Controller, you should now override supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation.

- (NSUInteger) supportedInterfaceOrientations {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    // Return the orientation you'd prefer - this is what it launches to. The
    // user can still rotate. You don't have to implement this method, in which
    // case it launches in the current orientation
    return UIInterfaceOrientationPortrait;
}

If you're targeting something older than iOS 6, you want the shouldAutorotateToInterfaceOrientation: method. By changing when it returns yes, you'll determine if it will rotate to said orientation. This will only allow the normal portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

    // Use this to allow upside down as well
    //return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

Note that shouldAutorotateToInterfaceOrientation: has been deprecated in iOS 6.0.

查看更多
登录 后发表回答