如何禁用横向方向? [英] How can I disable landscape orientation?
问题描述
我正在制作iPhone应用程序,我需要它处于纵向模式,因此如果用户侧向移动设备,它不会自动旋转。我该怎么做?
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?
推荐答案
要为特定的视图控制器禁用方向,你现在应该覆盖 supportedInterfaceOrientations
和 preferredInterfaceOrientationForPresentation
。
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;
}
如果你的目标是比iOS 6更早的东西,你想要 shouldAutorotateToInterfaceOrientation:
方法。通过更改何时返回yes,您将确定它是否将旋转到所述方向。这只允许正常的纵向方向。
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);
}
请注意 shouldAutorotateToInterfaceOrientation:
已在iOS 6.0中弃用。
Note that shouldAutorotateToInterfaceOrientation:
has been deprecated in iOS 6.0.
这篇关于如何禁用横向方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!