如何将我的应用程序限制为横向模式?

发布于 2024-09-30 23:16:35 字数 3064 浏览 3 评论 0原文

我使用 SplitView 模板创建了 iPad 应用程序。 我想知道将我的应用程序限制为横向模式的最佳方法是什么?

我尝试过重写 DetailViewController.m 中的 shouldAutorotateToInterfaceOrientation: 方法

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

,但 4.2 GM 仍然有问题并且无法显示控制器视图。我还有什么其他选择?

提前致谢。

UPDATE1

  • 我已经提交了错误报告: 错误 ID #8620135

  • 我的应用程序即将完成,我必须找到一个解决方法,因为我认为他们不会在 4.2 正式发布之前解决这个问题(GM 已经发布了!)

    为了重现该错误,只需使用 SplitView 模板并在任何 UIViewController(RootViewController 或 DetailViewControllers)中覆盖上述方法

UPDATE2< /strong>

我找到了解决方法。 (有关完整的解决方法,请参阅 UPDATE3)

仅设置 UISupportedInterfaceOrientations 以支持 Landscape ,这将强制应用程序以横向模式启动,从而允许 DetailViewController 正确启动(因此正确显示)

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

但是如果您旋转设备,它会变成纵向模式! !,所以仍然有必要重写 shouldAutorotateToIntercafeOrientation:如上所述

讨论:

如果这不是一个错误,我会收到警告或执行以视图控制器不支持的方向启动应用程序时出现错误、异常或其他问题。另外,为什么只有DetailViewController不显示?如果这是规范,那么 RootViewController 也应该无法加载。你不觉得吗? 感谢您的帮助...;)

UPDATE3

经过进一步的测试,我意识到上述解决方法在某些情况下不起作用。例如,当设备处于横向状态时启动应用程序将无法工作! 真正的问题似乎是,在 iOS4.2GM 中,UISplitViewController 需要其所有控制器在加载时都可以进行所有旋转。因此有必要欺骗他,使其以横向模式加载,然后不允许他旋转其视图控制器。

因此,这里是解决这个烦人的 iBug 的新方法。

步骤一: 像这样设置Info.plist:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

Step2 在 DetailViewController.m 或 .h(来自 SplitView 模板)中设置新标志

BOOL lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //WORK-ARROUND: Bug ID# 8620135.
    if (lockRotation) {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }else{
        return YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //set NO here since this is called before shouldAutorotateToInterfaceOrientation method is called
    lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.
}
- (void)viewDidAppear:(BOOL)animated {
    //set YES as soon as possible, but after shouldAutorotateToInterfaceOrientation method was called
    lockRotation = YES; //WORK-ARROUND: Bug ID# 8620135.
    [super viewDidAppear:animated];
}

重要提示: 请注意,此错误仅在加载 UISplitViewController 时出现,而不是每次都出现 其视图出现。因此,要查看此错误,请确保应用程序之前已终止。

I have my iPad application created using the SplitView template.
I wonder what is the best way to restrict my application to landscape mode?

I have tried overriding shouldAutorotateToInterfaceOrientation: method in DetailViewController.m

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

but 4.2 GM is still buggy and it fails to show the controller view. What other choices do I have?

Thanks in advance.

UPDATE1

  • I have already filed a bug report:
    Bug ID #8620135

  • My app is almost finished and I have to find a work-arround since I don't think they are going to solve this before 4.2 officially comes out (GM is already out!)

    In order to recreate the bug, just use SplitView template and override above method in any of the UIViewControllers (RootViewController or DetailViewControllers)

UPDATE2

I have found a work-around. (See UPDATE3 for the complete work-around)

Set UISupportedInterfaceOrientations only to support Landscape , this will force the app to start in landscape mode allowing DetailViewController to start correctly(hence shown correctly)

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

But if you rotate the device, it turns Portrait mode!!!, so is still necessary to override shouldAutorotateToIntercafeOrientation: as above

Discussion:

If this wouldn't be a bug I would expect a warning or execution error, exception or something when starting the app in a orientation that is not supported by the view controller. Besides, why only DetailViewController does not show? If this would be specification, then RootViewController should also fail to load then. Don't you think?
thanks for you help... ;)

UPDATE3

After further tests I have realized that above work-around does not work in some cases. For example when starting the app when the device is in landscape won't work!.
The real problem seems to be that in iOS4.2GM UISplitViewController needs all its controllers have all rotations to be available at its load time. So is necessary to trick him so it loads in Landscape mode and then not allow him to rotate its view controllers.

So here is the new work-around for this annoying iBug.

Step1:
Set Info.plist like so:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

Step2
Set a new flag in DetailViewController.m or .h (from SplitView Template)

BOOL lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //WORK-ARROUND: Bug ID# 8620135.
    if (lockRotation) {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }else{
        return YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //set NO here since this is called before shouldAutorotateToInterfaceOrientation method is called
    lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.
}
- (void)viewDidAppear:(BOOL)animated {
    //set YES as soon as possible, but after shouldAutorotateToInterfaceOrientation method was called
    lockRotation = YES; //WORK-ARROUND: Bug ID# 8620135.
    [super viewDidAppear:animated];
}

IMPORTANT NOTE:
Please note that this bug only appears when the UISplitViewController is loaded and not everytime
the its view appears. Hence, to see this bug make sure the app was terminated before.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑脸一如从前 2024-10-07 23:16:35

我问了一个赏金为 500 的问题 似乎与您面临的问题相同

根据我有限的经验,制作仅横向的 iPhone 应用程序比制作仅横向的 iPad 应用程序要容易得多。我不确定为什么会有任何差异,但苹果公司所说的使其仅横向显示的步骤本身并不起作用。

I asked a question with a bounty of 500 that seems to be the same thing you're facing.

From my limited experience it is much easier to make a landscape-only iPhone app than a landscape-only iPad app. I'm not sure why there is any difference, but the steps Apple says to take to make it landscape-only do not work on their own.

浊酒尽余欢 2024-10-07 23:16:35

试试这个(它有效):

-(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation {
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return YES;
    }
    else 
    {
        return NO;
    }
}

Try this (it works):

-(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation {
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return YES;
    }
    else 
    {
        return NO;
    }
}
Hello爱情风 2024-10-07 23:16:35

如果您还没有尝试过,请查看这个横向模式下的 iPhone 应用。我建议简单地将 UISupportedInterfaceOrientations 添加到您的 Info.plist 中并指定两个横向方向。但是,根据所引用问题的答案,显然这还不够。

Check out this iPhone app in landscape mode, if you haven't already. I was going to suggest simply adding UISupportedInterfaceOrientations to your Info.plist and specifying the two landscape orientations. But, apparently, this is not sufficient, according to answers to cited question.

女皇必胜 2024-10-07 23:16:35

我相信这是一个BUG,我也遇到过这个问题。 有关

UIInterfaceOrientationLandscapeLeft

这与复制这种情况

: 1)使用UISplitViewController模板创建一个新的iPad项目

2)编辑info.plist

Supported interface orientations
-Landscape (left home button)
-Landscape (right home button)

3)DetailViewController.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//    return YES;
 NSLog(@"RotateToInterface:[%d] vs LandscapeLeft[%d]", interfaceOrientation, UIInterfaceOrientationLandscapeLeft);
 return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}

4)运行它....您将看到一个空白的黑色视图。无论你如何转身。从未检测到“UIInterfaceOrientationLandscapeLeft”。

顺便说一句,nacho4d 的添加 BOOL 检查解决方法正在发挥作用。竖起大拇指 :)

I believe this is a BUG, I faced this problem also. It is something to do with

UIInterfaceOrientationLandscapeLeft

To replicate this situation:

1) Create a new iPad project using UISplitViewController template

2) Edit info.plist

Supported interface orientations
-Landscape (left home button)
-Landscape (right home button)

3) DetailViewController.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//    return YES;
 NSLog(@"RotateToInterface:[%d] vs LandscapeLeft[%d]", interfaceOrientation, UIInterfaceOrientationLandscapeLeft);
 return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}

4) Run it....You will see a blank black view. and no matter how you turn. "UIInterfaceOrientationLandscapeLeft" never detected.

By the way, nacho4d's adding BOOL check work-around is working. Thumbs UP :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文