我试图将整个应用的方向锁定为纵向模式,因此我将 info.plist
更改为:
但是,如果我旋转模拟器甚至我的真实设备, View 仍然会旋转。对此有什么解释吗?谢谢!
最佳答案
您需要在您的 ViewController 中实现它:
对于 iOS 6 及更高版本:
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
#endif
#endif
iOS 6 以下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL returnBool = YES;
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
returnBool = YES;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
returnBool = YES;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
returnBool = NO;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
returnBool = NO;
}
else {
returnBool = NO;
}
return returnBool;
}
预处理器语句是为了确保方法 __IPHONE_OS_VERSION_MAX_ALLOWED 存在
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
第二个:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
是为了确保设备上运行的 iOS 版本为 6 及以上,因为预处理器语句中的方法在 iOS 6 以下的版本上不存在。 如果没有预处理器语句,它会在 iOS 6 以下崩溃。
关于iOS 7 停止自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926154/