【问题标题】:iOS: Prevent View orientation from switching?iOS:防止视图方向切换?
【发布时间】:2023-03-27 00:38:02
【问题描述】:
在我的应用程序的构建设置中,我检查了所有支持的方向(除了倒置)。
但是,当我启动应用程序时,某些不打算以纵向显示的视图将自动旋转并截断一半视图。我不希望这些视图旋转。
如何防止这种情况发生?我试过使用:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
return YES;
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
但设备仍想尽可能地转为纵向。任何帮助都会很棒。
【问题讨论】:
标签:
iphone
ios
uiview
orientation
【解决方案1】:
此方法已弃用,不会在 iOS 6 中调用。有关更多信息,请参阅 Apple 的文档或关于 SO 的一千个问题之一以了解新方法。
【解决方案2】:
您不想旋转的视图是否嵌入在导航栏或标签栏中?如果是这样,我也面临同样的问题。您需要为标签栏/导航栏分配一个类,并将 ios 6 的新方法覆盖为:
-(BOOL)shouldAutoRotate
{
return NO;
}
如果这是您遇到的问题,请告诉我。我还必须使用 NSNotificationCenter 实现方向更改,以便允许在我的一个子视图上进行旋转。如果这是你要找的东西,我会发布。
告诉我!
【解决方案3】:
shouldAutorotateToInterfaceOrientation 已弃用。新的替换是shouldAutoRotate。为了在返回shouldAutoRotate 的值之前在界面方向之间进行descern,请使用UIViewControllers 的interfaceOrientation 属性。例如,你可以有这样的东西:
-(BOOL)shouldAutoRotate{
if (self.interfaceOrientation == UIInterfaceOrientationPortrait){
return YES;
}
else {
return NO;
}
}