如何禁用横向方向?

时间:2022-07-03 21:09:56

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?

我正在制作一个iPhone应用程序,我需要它处于纵向模式,所以如果用户侧向移动设备,它不会自动旋转。我怎样才能做到这一点?

5 个解决方案

#1


51  

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

要禁用特定View Controller的方向,您现在应该覆盖supportedInterfaceOrientations和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.

如果您的目标是iOS 6之前的版本,则需要使用shouldAutorotateToInterfaceOrientation:方法。通过更改何时返回yes,您将确定它是否将旋转到所述方向。这将只允许正常的纵向方向。

- (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.

请注意,在iOS 6.0中已弃用shouldAutorotateToInterfaceOrientation :.

#2


37  

Xcode 5 and above

  • Click your project in the Project Navigator in the left sidebar to open the project settings
  • 在左侧边栏的Project Navigator中单击您的项目以打开项目设置
  • Go to the General tab.
  • 转到“常规”选项卡。
  • Uncheck the options you don't want in the Deployment Info section, under Device Orientation
  • 在“设备方向”下的“部署信息”部分中取消选中您不需要的选项

如何禁用横向方向?

#3


28  

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):

对于那些错过它的人:您可以使用项目设置屏幕来修复整个应用程序的方向(无需覆盖每个控制器中的方法):

如何禁用横向方向?

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.

它就像切换支持的接口方向一样简单。您可以通过单击左侧面板中的项目>应用程序目标>摘要选项卡找到。

#4


1  

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

Swift 3如果您有一个navigationController,则将其子类化为此类(仅限肖像):

class CustomNavigationViewController: UINavigationController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
  }

  override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIInterfaceOrientation.portrait
  }
}

#5


0  

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.

删除方法shouldAutorotateToInterfaceOrientation从你的班级完全也可以工作。如果你不计划旋转,那么在你的课程中使用该方法是没有意义的,代码越少越好,保持干净。

#1


51  

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

要禁用特定View Controller的方向,您现在应该覆盖supportedInterfaceOrientations和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.

如果您的目标是iOS 6之前的版本,则需要使用shouldAutorotateToInterfaceOrientation:方法。通过更改何时返回yes,您将确定它是否将旋转到所述方向。这将只允许正常的纵向方向。

- (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.

请注意,在iOS 6.0中已弃用shouldAutorotateToInterfaceOrientation :.

#2


37  

Xcode 5 and above

  • Click your project in the Project Navigator in the left sidebar to open the project settings
  • 在左侧边栏的Project Navigator中单击您的项目以打开项目设置
  • Go to the General tab.
  • 转到“常规”选项卡。
  • Uncheck the options you don't want in the Deployment Info section, under Device Orientation
  • 在“设备方向”下的“部署信息”部分中取消选中您不需要的选项

如何禁用横向方向?

#3


28  

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):

对于那些错过它的人:您可以使用项目设置屏幕来修复整个应用程序的方向(无需覆盖每个控制器中的方法):

如何禁用横向方向?

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.

它就像切换支持的接口方向一样简单。您可以通过单击左侧面板中的项目>应用程序目标>摘要选项卡找到。

#4


1  

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

Swift 3如果您有一个navigationController,则将其子类化为此类(仅限肖像):

class CustomNavigationViewController: UINavigationController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
  }

  override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIInterfaceOrientation.portrait
  }
}

#5


0  

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.

删除方法shouldAutorotateToInterfaceOrientation从你的班级完全也可以工作。如果你不计划旋转,那么在你的课程中使用该方法是没有意义的,代码越少越好,保持干净。