我更新到 Xcode 4.5 并正在使用 iOS6——这是一个我肯定会犯的错误not下次有更新时进行;对于刚接触 iOS 的人来说,这有点像噩梦——而且我刚刚注意到我正在开发的一个应用程序正在自动旋转。我在更新之前从未注意到它会自动旋转,但也有可能我在测试时没有旋转手机,所以我不能确定。我已将以下代码添加到主 UIViewController 并且它仍在旋转:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
这是禁用自动旋转的正确方法吗?如果是的话,那么 iOS6 可能会有一些变化,我必须等到完整版本才能找到答案。但如果我弄错了,我应该使用什么代码呢?
一如既往地感谢您的帮助。
EDIT:这是我将其更改为的代码,但它仍在旋转。难道是我听错了?
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
return YES;
}
else
{
return NO;
}
}
那是因为从来没有成功过。您应该选择其中一个方向。
按住命令并单击 UIInterfaceOrientation 您将看到可能选项的枚举。
然后你可以针对这些进行测试来决定你的YES
设想。
我最初可能误解了你的问题。看来您可能一直在说您的应用程序允许旋转。但代码应该不允许这样做。
我以为你是说它仍在触发代码。试图找到一个“是”
需要考虑的一件事。是否可能有多个可用的视图控制器。也许你的代码没有被击中。
有几个可能的问题。
您的代码甚至没有被使用。因为视图被分配为 UIViewController 而不是您的自定义视图控制器。
您的代码正在被使用,但视图控制器不是被询问方向的控制器。因此该特定代码不会被击中。
糟糕的构建会不断地将错误的组件放入设备上。
您的解决方案可以如下。
确保您的代码是被分配的代码。要么对您的自定义类进行直接分配。或者 xib 文件正在膨胀它。打开 xib 文件后,请检查 Identity Inspector。选择您的视图控制器并确保自定义类设置为您的类类型
看看层次结构。还有哪些其他视图控制器。也许其中之一告诉应用程序它可以自动旋转到任何方向。
找到“DerivedData”文件夹并将其完全删除。有时这是组织者的工作。其他时候你需要直接从磁盘上删除。然后清理并再次构建。
另外,另一种解决方案可以像在项目文件中设置设置一样简单。
从文件浏览器中选择您的项目文件,您将在摘要中看到 iPad 和 iPod 设置。您可以针对要禁止的方向“取消按下”按钮。以及您没有以其他方式编码方向的任何视图控制器。默认情况下将使用这些。
对于造成的混乱,我深表歉意。
Update
我通常使用此代码来处理我的自动旋转。
它不仅将 ipad 与其他 ios 设备区分开来,而且还将请求转发到所呈现的控制器上,以便以模态显示的视图可以按照其想要的方式做出响应。
当你不理解时,定位是一种痛苦:)
// Detect iPad
#define IS_IPAD() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad : NO)
// Set preferred orientation for initial display
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
if (IS_IPAD()){
return UIInterfaceOrientationLandscapeRight;
}
else {
return UIInterfaceOrientationPortrait;
}
}
// Return list of supported orientations.
- (NSUInteger)supportedInterfaceOrientations{
if (self.presentedViewController != nil){
return [self.presentedViewController supportedInterfaceOrientations];
}
else {
if (IS_IPAD()){
return UIInterfaceOrientationMaskLandscapeRight;
}
else {
return UIInterfaceOrientationMaskAll;
}
}
}
// Determine iOS 6 Autorotation.
- (BOOL)shouldAutorotate{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
// Return yes to allow the device to load initially.
if (orientation == UIDeviceOrientationUnknown) return YES;
// Pass iOS 6 Request for orientation on to iOS 5 code. (backwards compatible)
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
return result;
}
// handle iOS 5 Orientation as normal
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
// Return YES for supported orientations
if (self.presentedViewController != nil){
return [self.presentedViewController shouldAutorotate];
}
else {
if (IS_IPAD()){
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)