在iOS5 / 6中自动旋转? [英] auto-rotation in iOS5/6?

查看:151
本文介绍了在iOS5 / 6中自动旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新到Xcode 4.5并且正在使用iOS6 - 这是一个错误,我肯定下次有更新;对于那些刚接触iOS的人来说,这有点噩梦 - 我刚刚注意到我正在研究的一款应用就是自动旋转。我在更新之前从未注意到它的自动旋转,但也有可能我在测试时没有旋转手机,所以我不能确定。我已将以下代码添加到主UIViewController并且它仍在旋转:

   - (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
返回NO;
}

这是禁用自动旋转的正确方法吗?如果是,那么iOS6可能会有一些变化,我将不得不等到完整版本才能找到答案。但如果我弄错了,我应该使用什么代码?



一如既往地感谢您的帮助。



编辑:以下是我将其更改为的代码,但它仍在旋转。我弄错了吗?

   - (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait)
{
return YES;
}
其他
{
返回NO;
}
}


解决方案

那个是因为从来没有成功过。您应该选择其中一个方向。



按住命令并单击UIInterfaceOrientation,您将看到可能选项的枚举。



然后你可以测试那些决定你的 YES 情景。



我原本可能误解了你的问题。您可能一直在说您的应用允许轮换。但代码应该禁止这样做。



我以为你说它仍在解雇代码。试图找到是



要考虑一件事。可能有多个视图控制器可用。也许你的代码没有被击中。



这可能有几个问题。


  1. 您的代码甚至没有被使用。因为视图被分配为UIViewController而不是自定义视图控制器。


  2. 您正在使用代码,但View控制器不是被询问的那个方向。因此,特定代码没有被点击。


  3. 错误的构建不断将错误的程序集放到设备上。


您的解决方案如下:


  1. 确保您的代码是被分配的代码。您的自定义类有直接分配。或者xib文件正在膨胀它。打开xib文件后,请查看Identity Inspector。选择您的View Controller并确保将自定义类设置为您的类类型


  2. 查看层次结构。还有其他视图控制器。也许其中一个人告诉应用它可以自动转向任何方向。


  3. 找到你的DerivedData文件夹并完全删除它。有时这可以从组织者那里获得。其他时候你需要直接删除磁盘。然后再次清理并构建。


另一种解决方案可能就像设置项目文件中的设置一样简单。 / p>

从文件浏览器中选择项目文件,您将在摘要中看到iPad和iPod设置。您可以使用取消按下按钮来选择要禁用的方向。和任何你没有编码方向的视图控制器。默认会使用这些。



对于混淆我道歉。



更新



我通常使用此代码来处理我的自动旋转。



它不仅将ipad与其他ios设备区分开来,而且还将请求转发到呈现的控制器上,因此以模态显示的视图可以响应它的需要。



如果您不理解,定位会很痛苦:)

  / /检测iPad 
#define IS_IPAD()([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)]?\
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad:NO)

//设置初始显示的首选方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if(IS_IPAD()){
返回UIInterfaceOrientationLandscapeRight;
}
else {
返回UIInterfaceOrientationPortrait;
}
}
//返回支持的方向列表。
- (NSUInteger)supportedInterfaceOrientations {
if(self.presentedViewController!= nil){
return [self.presentedViewController supportedInterfaceOrientations];
}
else {
if(IS_IPAD()){
返回UIInterfaceOrientationMaskLandscapeRight;
}
else {
返回UIInterfaceOrientationMaskAll;
}
}
}

//确定iOS 6自动旋转。
- (BOOL)shouldAutorotate {
UIDeviceOrientation orientation = [UIDevice currentDevice] .orientation;
//返回yes以允许设备最初加载。
if(orientation == UIDeviceOrientationUnknown)返回YES;
//传递iOS 6请求定位到iOS 5代码。 (向后兼容)
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
返回结果;
}
//处理iOS 5方向正常
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//返回YES表示支持的方向

if(self.presentedViewController!= nil){
return [self.presentedViewController shouldAutorotate];
}
else {
if(IS_IPAD()){
return(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else {
return(interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
}


I updated to Xcode 4.5 and am working with iOS6--a mistake I will definitely not make next time there's an update; it's been sort of nightmarish for somebody so new to iOS--and I've just noticed an app I'm working on is autorotating. I never noticed it autorotatin before the update, but it's also possible I just didn't rotate the phone while testing, so I can't be sure. I've added the following code to the main UIViewController and it's still rotating:

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

Is this the right way to disable autorotation? If it is, then maybe there's some change in iOS6 and I'll have to wait until the full release to find out. But if I've gotten it wrong, what code should I use instead?

Thanks, as always, for your help.

EDIT: Here's the code I changed it to, but it's still rotating. Have I gotten it wrong?

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
    return YES;
}
else 
{
    return NO;
}
}

解决方案

that is because there was never a success. You should choose one of the orientations.

Hold command and click on UIInterfaceOrientation you will see an enumeration of the possible options.

then you can test against those to decide your YES Scenario.

I may have originally misunderstood your problem. It seems that you may have been saying that your app is allowing rotation. but the code should disallow that.

I was thinking you were saying it was still firing the code. Trying to find a Yes

One thing to think about. is there may be more than one view controller available. perhaps your code is not being hit.

A couple of possible issues for this.

  1. Your code is not even being used. because the view is being allocated as UIViewController as opposed to your custom view controller.

  2. You code is being used but that View controller is not the one being asked about the Orientation. therefore that specific code is not being hit.

  3. A bad build keeps putting the wrong assemblies onto the device.

Your solutions can be as follows.

  1. Ensure your code is the one being allocated. Either there is a direct alloc on your custom class. or the xib file is inflating it. Check out the Identity Inspector when you have your xib file open. select your View Controller and ensure that custom class is set to your class type

  2. Look at the hierarchy. what other view controllers are there. Perhaps one of those are telling the app it can autorotate to any orientation.

  3. Find your "DerivedData" folder and remove it entirely. Sometimes this works from the organizer. other times you need to delete directly off the disk. Then clean and build again.

Also another solution could be as simple as setting the settings in the Project file.

Select your project file from the file browser and you will see the iPad and iPod settings in the summary. You can "UnPress" buttons for the orientations that you want to disallow. and any view controllers that you do not otherwise code orientation into. will use these by default.

My apologies for the confusion.

Update

I commonly use this code to handle my autorotation.

It not only differentiates the ipad from the other ios devices, but it also forwards the request onto presented controllers so a view that is shown modal may respond how it wants.

Orientation is a pain when you dont understand it :)

// 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);
        }
    }
}

这篇关于在iOS5 / 6中自动旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆