XCode 4.5 / iOS6:横向模式下我的 iPad 应用程序的方向问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12717951/
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
XCode 4.5 / iOS6 : orientation issue with my iPad app in Landscape mode
提问by Regis_AG
I just opened my iPad project on XCode 4.5. My app is designed to run in Landscape mode. It works perfectly on previous versions of XCode. But on XCode 4.5, it is rotated of 90° (quarter turn) with a empty area on the right side of the screen (and my view has the correct size but goes out of the screen). It looks like this:
我刚刚在 XCode 4.5 上打开了我的 iPad 项目。我的应用程序旨在以横向模式运行。它适用于以前版本的 XCode。但是在 XCode 4.5 上,它旋转了 90°(四分之一圈),屏幕右侧有一个空白区域(我的视图具有正确的大小但超出了屏幕)。它看起来像这样:
I checked the following posts but didn't help:
我检查了以下帖子,但没有帮助:
ios6 Rotation issue from landscape to portrait mode
Set orientation to landscape mode in xcode 4.5 GM IOS 6
在 xcode 4.5 GM IOS 6 中将方向设置为横向模式
Anybody had this issue ?
有人遇到过这个问题吗?
Any help would be appreciated !
任何帮助,将不胜感激 !
采纳答案by Regis_AG
Thanks everybody for your replies. I finally found a solution.
谢谢大家的回复。我终于找到了解决方案。
First, check that all your launch images have the correct orientation and correct size in the target summary menu (blue icon of your project => target => summary => scroll at the bottom) ; if the size or orientation is not correct, you get a warning on the launch image which is not set properly.
首先,检查目标摘要菜单中所有启动图像的方向和大小是否正确(项目的蓝色图标 => 目标 => 摘要 => 在底部滚动);如果尺寸或方向不正确,您会在未正确设置的启动图像上收到警告。
Up to now, I had a project with this structure (old XCode fashion):
到目前为止,我有一个具有这种结构的项目(旧的 XCode 方式):
AppDelegate.h
and .mRootViewController.h
and .m- a
MainWindow-Iphone.xib
andMainWindow-iPad.xib
(with theRootViewController
linked in Interface Builder ; see the screenshot below with the yellow/brown icon (with a square inside) relative to theRootViewController
)
AppDelegate.h
和.mRootViewController.h
和.m- a
MainWindow-Iphone.xib
和MainWindow-iPad.xib
(RootViewController
在 Interface Builder 中带有链接;请参阅下面的屏幕截图,其中带有相对于 的黄色/棕色图标(里面有一个正方形)RootViewController
)
Here below a screenshot of what it looked like:
下面是它的外观截图:
And here is what was the code in the applicationDidFinishLaunching: method of my AppDelegate:
这是我的 AppDelegate 的 applicationDidFinishLaunching: 方法中的代码:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:[rootViewController view]];
[window makeKeyAndVisible];
}
What I did is to be closer to the structure you get when you create an empty project with XCode 4.5. Consequently:
我所做的是更接近使用 XCode 4.5 创建空项目时获得的结构。最后:
- I removed the
MainWindow.xib
andMainWindow-iPad.xib
and now created my window programatically (clearer and better to make sure that it fits the size of any screen) - I removed the "Main nib file base name" and "Main nib file base name (iPad)" keys which were defined in my
Info.plist
(and set toMainWindow.xib
andMainWindow-iPad.xib
) - I added empty
RootViewController_iPhone.xib
andRootViewController_iPad.xib
I changed the code in my
applicationDidFinishLaunching
method like this:- (void)applicationDidFinishLaunching:(UIApplication *)application { NSLog(@"applicationDidFinishLaunching"); self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPhone" bundle:nil]; } else { self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad" bundle:nil]; } self.window.rootViewController = self.rootViewController; [self.window makeKeyAndVisible];
}
- 我删除了
MainWindow.xib
并且MainWindow-iPad.xib
现在以编程方式创建了我的窗口(更清晰,更好地确保它适合任何屏幕的大小) - 我删除了在我的
Info.plist
(并设置为MainWindow.xib
和MainWindow-iPad.xib
)中定义的“主要笔尖文件基本名称”和“主要笔尖文件基本名称(iPad)”键 - 我添加了空
RootViewController_iPhone.xib
和RootViewController_iPad.xib
我
applicationDidFinishLaunching
像这样更改了我的方法中的代码:- (void)applicationDidFinishLaunching:(UIApplication *)application { NSLog(@"applicationDidFinishLaunching"); self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPhone" bundle:nil]; } else { self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad" bundle:nil]; } self.window.rootViewController = self.rootViewController; [self.window makeKeyAndVisible];
}
And now, everything works fine ! Orientation is correct on iPad ! And it is much more clear than before :) I even didn't have to change the deprecated methods like
现在,一切正常!iPad 上的方向是正确的!而且比以前清楚得多:)我什至不必更改已弃用的方法,例如
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
By the way, to make sure that all your views will be full screen (on iPhone 5) make sure that your views are set to the mode "Scale to fill" in Interface Builder and that "Autoresize subviews" is clicked. If some of your views do not scale to full screen, it is probably due to the order in which one you create your controllers/views (the superView sends a notification to its subviews only when it (the superView) is created). To solve this easily, simply add the following code in the - (void)viewDidLoad method
:
顺便说一下,要确保所有视图都是全屏的(在 iPhone 5 上),请确保在 Interface Builder 中将视图设置为“缩放以填充”模式,并单击“自动调整子视图大小”。如果您的某些视图没有缩放到全屏,则可能是由于您创建控制器/视图的顺序(superView 仅在创建它(superView)时才向其子视图发送通知)。要轻松解决此问题,只需在以下代码中添加以下代码- (void)viewDidLoad method
:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
[self.view setFrame:CGRectMake(0,0,screenBounds.size.width,screenBounds.size.height)];
or use:
或使用:
[self presentModalViewController:myViewController animated:TRUE];
instead of:
代替:
[self.view.superview addSubview:myViewController.view];
presentModalViewController
indeed sends a resizing notification to the subviews.
presentModalViewController
确实向子视图发送调整大小通知。
Hope this will help !
希望这会有所帮助!
回答by Steve-o
Make sure you are setting the window.rootViewController as well. I had the same issue, but this line fixed it for me:
确保您也在设置 window.rootViewController。我有同样的问题,但这条线为我解决了这个问题:
MainViewController *mainView = [[MainViewController alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = mainView;
回答by Anthony
I had a similar problem on an app I upgraded. I haven't found it documented but it seems there has been a change. What I ended up finding is the new window doesn't seem to know about rotation or size until after it is made key and visible. I was able to move my frame setting to immediately after makeKeyAndVisible and everything worked. I hope that helps you.
我在升级的应用程序上遇到了类似的问题。我还没有发现它记录在案,但似乎发生了变化。我最终发现的是,新窗口在成为关键和可见之前似乎并不知道旋转或大小。我能够在 makeKeyAndVisible 之后立即将我的框架设置移动到并且一切正常。我希望这对你有帮助。
回答by Angelo Palma
[[yourViewController view] setFrame:CGRectMake(0.0, 0.0, 1024.0, 768.0)];
[[yourViewController view] setTransform:CGAffineTransformTranslate(CGAffineTransformMakeRotation(0.5*M_PI),128.0,128.0)];
回答by NRR
If you are using Storyboards one easy hack is to use the loadView method. It is called before viewDidLoad. Just go to your Storyboard and delete the associated view as you are going to create it programmatically in loadView. Then return to the view controller class and copy the following code:
如果您使用 Storyboards,一个简单的技巧就是使用 loadView 方法。它在 viewDidLoad 之前调用。只需转到您的 Storyboard 并删除关联的视图,因为您将在 loadView 中以编程方式创建它。然后返回视图控制器类,复制以下代码:
- (void)loadView
{
// You can adjust the view size and location here. If using full screen use the following code. If you have a tab bar for example and also want to account for the top default iPad bar, use: CGRectMake(0, 20, 1024, 699) instead.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
view.backgroundColor = [UIColor whiteColor];
view.autoresizesSubviews = NO;
self.view = view;
}
回答by codeburn
A few simple steps in order will solve this problem for you.
只需按顺序执行几个简单的步骤即可为您解决这个问题。
First, in AppDelegate.m, check if you're adding your rootViewController as a subView. That is, instead of this,
首先,在 AppDelegate.m 中,检查您是否将 rootViewController 添加为子视图。也就是说,而不是这个,
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
do something like this
做这样的事情
- (void)applicationDidFinishLaunching:(UIApplication *)application {
window.rootViewController = navigationController;
[window makeKeyAndVisible];
}
if you're setting a navigation controller as your root view controller.
如果您将导航控制器设置为根视图控制器。
Second, if you need to get control of the rotation methods within your navigationController's pushed viewControllers, create a category for UINavigationController like so:
其次,如果您需要控制导航控制器推送的视图控制器中的旋转方法,请为 UINavigationController 创建一个类别,如下所示:
#import "UINavigationController+Rotation.h"
@implementation UINavigationController (Rotation)
- (BOOL)shouldAutorotate {
BOOL result = self.topViewController.shouldAutorotate;
return result;
}
- (NSUInteger)supportedInterfaceOrientations {
return self.topViewController.supportedInterfaceOrientations;
}
@end
Now, these two orientation methods for iOS 6 upwards
现在,iOS 6 向上的这两种定位方法
-(BOOL)shouldAutorotateand
-(NSUInteger)supportedInterfaceOrientations
-(BOOL)shouldAutorotate和
-(NSUInteger)supportedInterfaceOrientations
will get called within your classes.
将在您的课程中被调用。
This is necessary because the older rotation methods such as
这是必要的,因为旧的旋转方法,例如
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
have been deprecated from iOS 6.
已从 iOS 6 弃用。
So lastly, you'll need to implement these in your view controllers; something like this:
所以最后,你需要在你的视图控制器中实现这些;像这样:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
if you want to allow your view controller to rotate and support all orientations.
如果你想让你的视图控制器旋转并支持所有方向。
The other orientation masks are:
其他方向掩码是:
- UIInterfaceOrientationMaskPortrait
- UIInterfaceOrientationMaskLandscapeLeft
- UIInterfaceOrientationMaskLandscapeRight
- UIInterfaceOrientationMaskPortraitUpsideDown
- UIInterfaceOrientationMaskLandscapeand
- UIInterfaceOrientationMaskAllButUpsideDown
- UIInterfaceOrientationMaskPortrait
- UIInterfaceOrientationMaskLandscapeLeft
- UIInterfaceOrientationMaskLandscapeRight
- UIInterfaceOrientationMaskPortraitUpsideDown
- UIInterfaceOrientationMaskLandscape和
- UIInterfaceOrientationMaskAllButUpsideDown