ios 横向模式的 iPhone 应用程序,2008 系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:13:42  来源:igfitidea点击:

iPhone app in landscape mode, 2008 systems

iosobjective-clandscape

提问by Michiel de Mare

Please note that this question is from 2008 and now is of only historic interest.

请注意,这个问题来自 2008 年,现在仅具有历史意义。



What's the best way to create an iPhone application that runs in landscape mode from the start, regardless of the position of the device?

无论设备的位置如何,创建从一开始就以横向模式运行的 iPhone 应用程序的最佳方法是什么?

Both programmatically and using the Interface Builder.

以编程方式和使用界面生成器。

采纳答案by Fattie

Historic answer only. Spectacularly out of date.

仅历史答案。非常过时。

Please note that this answer is now hugely out of date/

请注意,这个答案现在已经过时了/

This answer is only a historical curiosity.

这个答案只是一种历史好奇



Exciting news! As discovered by Andrew below, this problem has been fixed by Apple in 4.0+.

令人兴奋的消息!正如下面的 Andrew 所发现的,Apple 在 4.0+ 中已经修复了这个问题。

It would appear it is NO longer necessary to force the size of the view on every view, and the specific serious problem of landscape "only working the first time" has been resolved.

似乎不再需要在每个视图上强制视图的大小,并且景观“只在第一次工作”的具体严重问题已经解决。

As of April 2011, it is not possible to test or even build anything below 4.0, so the question is purely a historic curiosity. It's incredible how much trouble it caused developers for so long!

截至 2011 年 4 月,无法测试甚至构建低于 4.0 的任何内容,因此这个问题纯粹是历史性的好奇心。令人难以置信的是,它给开发人员带来了这么长时间的麻烦!



Here is the original discussion and solution. This is utterly irrelevant now, as these systems are not even operable.

这是原始讨论和解决方案。现在这完全无关紧要,因为这些系统甚至无法运行。



It is EXTREMELY DIFFICULT to make this work fully -- there are at least three problems/bugs at play.

使这项工作完全发挥作用是非常困难的——至少有三个问题/错误在起作用。

try this .. interface builder landscape design

试试这个..界面构建器景观设计

Note in particular that where it says "and you need to use shouldAutorotateToInterfaceOrientation properly everywhere"it means everywhere, all your fullscreen views.

特别要注意,它说“你需要在任何地方正确使用 shouldAutorotateToInterfaceOrientation”,它意味着无处不在,你所有的全屏视图。

Hope it helps in this nightmare!

希望它有助于这个噩梦!

An important reminder of the ADDITIONAL well-known problem at hand here: if you are trying to swap between MORE THAN ONEview (all landscape), IT SIMPLY DOES NOT WORK. It is essential to remember this or you will waste days on the problem. It is literally NOT POSSIBLE. It is the biggest open, known, bug on the iOS platform. There is literally no way to make the hardware make the second view you load, be landscape. The annoying but simple workaround, and what you must do, is have a trivial master UIViewController that does nothing but sit there and let you swap between your views.

在这里手的附加众所周知的问题的一个重要的提示:如果你之间试图交换不止一个视图(所有横向),它根本不起作用。记住这一点很重要,否则你会在这个问题上浪费几天时间。从字面上看这是不可能的。这是 iOS 平台上最大的公开、已知错误。实际上没有办法让硬件成为你加载的第二个视图,是横向的。烦人但简单的解决方法,以及你必须做的,是有一个微不足道的主 UIViewController,它只是坐在那里让你在视图之间切换。

In other words, in iOS because of a major know bug:

换句话说,在 iOS 中,由于一个主要的已知错误:

[window addSubview:happyThing.view];
[window makeKeyAndVisible];

You can do that only once. Later, if you try to remove happyThing.view, and instead put in there newThing.view, IT DOES NOT WORK - AND THAT'S THAT. The machine will never rotate the view to landscape. There is no trick fix, even Apple cannot make it work. The workaround you must adopt is having an overall UIViewController that simply sits there and just holds your various views (happyThing, newThing, etc). Hope it helps!

你只能这样做一次。稍后,如果您尝试删除happyThing.view,而是将newThing.view 放入其中,则它不起作用-就是这样。机器永远不会将视图旋转为横向。没有技巧修复,即使是 Apple 也无法让它工作。您必须采用的解决方法是拥有一个整体的 UIViewController,它只是坐在那里并只保存您的各种视图(happyThing、newThing 等)。希望能帮助到你!

回答by Michael Pryor

From the Apple Dev Site:

来自 Apple 开发站点:

To start your application in landscape mode so that the status bar is in the appropriate position immediately, edit your Info.plist file to add the UIInterfaceOrientation key with the appropriate value (UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft), as shown in Listing 2.

Listing 2: Starting your application in landscape mode

<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeRight</string>

要以横向模式启动您的应用程序以便状态栏立即位于适当的位置,请编辑您的 Info.plist 文件以添加具有适当值(UIInterfaceOrientationLandscapeRight 或 UIInterfaceOrientationLandscapeLeft)的 UIInterfaceOrientation 键,如清单 2 所示。

清单 2:以横向模式启动应用程序

<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeRight</string>

回答by IlDan

Summary and integration from all the posts, after testing it myself; check the update for 4.x, 5.x below.

所有帖子的总结和整合,经过我自己的测试;检查下面 4.x、5.x 的更新。

As of 3.2 you cannot change the orientation of a running application from code.

从 3.2 开始,您无法通过代码更改正在运行的应用程序的方向。

But you can start an application with a fixed orientation, although doing so this is not straightforward.

但是您可以以固定方向启动应用程序,尽管这样做并不简单。

Try with this recipe:

试试这个食谱:

  1. set your orientation to UISupportedInterfaceOrientationsin the Info.plistfile
  2. in your window define a 480x320 "base view controller". Every other view will be added as a subview to its view.
  3. in all view controllers set up the shouldAutorotateToInterfaceOrientation:method (to return the same value you defined in the plist, of course)
  4. in all view controllers set a background view with

    self.view.frame = CGRectMake(0, 0, 480, 320)

    in the viewDidLoadmethod.

  1. 将您的方向设置为UISupportedInterfaceOrientationsInfo.plist文件中
  2. 在您的窗口中定义一个 480x320 的“基本视图控制器”。每个其他视图都将作为子视图添加到其视图中。
  3. 在所有视图控制器中设置shouldAutorotateToInterfaceOrientation:方法(当然,返回您在 plist 中定义的相同值)
  4. 在所有视图控制器中设置一个背景视图

    self.view.frame = CGRectMake(0, 0, 480, 320)

    viewDidLoad方法。

Update (iOS 4.x, 5.x): the Apple iOS App Programming Guide has a "Launching in Landscape Mode" paragraph in the "Advanced App Tricks" chapter.

更新(iOS 4.x、5.x):Apple iOS App Programming Guide 在“Advanced App Tricks”一章中有“Launching in Landscape Mode”段落。

References:

参考:

回答by samvermette

First I set in info.plist

首先我在 info.plist 中设置

<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeRight</string>

then I put this code in applicationDidFinishLaunching:

然后我把这段代码放在 applicationDidFinishLaunching 中:

CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57079633);
[window setTransform:rotate];

CGRect contentRect = CGRectMake(0, 0, 480, 320); 
window.bounds = contentRect; 
[window setCenter:CGPointMake(160.0f, 240.0f)]; 

This way I can work on the view in Interface Builder in landscape mode.

这样我就可以在横向模式下在 Interface Builder 中处理视图。

回答by S?ren Kuklau

sasb's and michaelpryor's answer appears to be correct, but if it's not working for you, try this alternative:

sasb 和 michaelpryor 的答案似乎是正确的,但如果它不适合您,请尝试以下替代方法:

- (void)applicationDidFinishLaunchingUIApplication *)application {
    application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}

Or this one:

或者这个:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

Or this one:

或者这个:

[application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];

You may also have to call window makeKeyAndVisible;first.

您可能还需window makeKeyAndVisible;要先打电话。

A few links: Developing in landscape mode, iPhone SDK: How to force Landscape mode only?

几个链接:以横向模式开发iPhone SDK:如何仅强制横向模式?

@Robert: please refer to The iPhone SDK, NDA, and Stack Overflow.

@Robert:请参阅iPhone SDK、保密协议和堆栈溢出

回答by Arlen Anderson

I'm surprised no one has come up with this answer yet:

我很惊讶还没有人想出这个答案:

In all my tests when a dismissing a modal view controller the parent view controller's preferred orientation set in shouldAutorotateToInterfaceOrientationis honored even when part of a UINavigationController. So the solution to this is simple:

在我的所有测试中,当关闭模态视图控制器shouldAutorotateToInterfaceOrientation时,即使是 UINavigationController 的一部分,父视图控制器的首选方向设置也会得到尊重。所以解决这个问题很简单:

Create a dummy UIViewController with a UIImageView for a background. Set the image to the default.png image your app uses on startup.

创建一个带有 UIImageView 的虚拟 UIViewController 作为背景。将图像设置为您的应用在启动时使用的 default.png 图像。

When viewWillAppeargets called in your root view controller, just present the dummy view controller without animation.

viewWillAppear在你的根视图控制器中被调用时,只呈现没有动画的虚拟视图控制器。

when viewDidAppeargets called in your dummy view controller, dismiss the view controller with a nice cross dissolve animation.

viewDidAppear在你的虚拟视图控制器中被调用时,用一个漂亮的交叉溶解动画关闭视图控制器。

Not only does this work, but it looks good! BTW, just for clarification i do the root view controller's viewWillAppearlike this:

这不仅有效,而且看起来不错!顺便说一句,为了澄清起见,我viewWillAppear像这样执行根视图控制器:

- (void)viewWillAppear:(BOOL)animated
{
 if ( dummy != nil ) {
  [dummy setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
  [self presentModalViewController:dummy animated:NO];
  [dummy release];
  dummy = nil;
 }
...
}

回答by Arlen Anderson

The latest iPhone OS Programming Guide has a full section on this, with sample code. I am sure this is a recent addition, so maybe you missed it. It explains all the conditions you have to comply with; basically...

最新的 iPhone OS 编程指南有一个完整的部分,带有示例代码。我确定这是最近添加的,所以也许你错过了。它解释了您必须遵守的所有条件;基本上...

  • set the Info.plist properties (this changes the position of the status bar, but not the view)
  • rotate your view manually around its center, on either your UIViewController viewDidLoad: method or your applicationDidFinishLaunching: method or implement auto rotation ("Autoresizing behaviors", page 124)
  • 设置 Info.plist 属性(这会更改状态栏的位置,但不会更改视图)
  • 在 UIViewController viewDidLoad: 方法或 applicationDidFinishLaunching: 方法上围绕其中心手动旋转视图或实现自动旋转(“自动调整大小行为”,第 124 页)

Look for "Launching in Landscape Mode", page 102.

查找“以横向模式启动”,第 102 页。

回答by nicc

See this answer: Landscape Mode ONLY for iPhone or iPad

请参阅此答案:仅适用于 iPhone 或 iPad 的横向模式

  1. add orientation to plist
  2. shouldAutorotateToInterfaceOrientation = YES in all files
  1. 向 plist 添加方向
  2. shouldAutorotateToInterfaceOrientation = YES 在所有文件中

Although if you're using mixed modes, you might be better off with

虽然如果你使用混合模式,你可能会更好

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];