XCode 4.5/iOS6:我的iPad应用程序在横向模式下的定位问题
我刚刚在XCode 4.5上打开了我的iPad项目。我的应用程序设计为以横向模式运行。它在以前版本的XCode上工作得非常好。但在XCode 4.5上,它旋转了90°(四分之一圈),屏幕右侧有一个空白区域(我的视图大小正确,但超出了屏幕)。看起来是这样的:XCode 4.5/iOS6:我的iPad应用程序在横向模式下的定位问题,ios,xcode,ipad,ios6,xcode4.5,Ios,Xcode,Ipad,Ios6,Xcode4.5,我刚刚在XCode 4.5上打开了我的iPad项目。我的应用程序设计为以横向模式运行。它在以前版本的XCode上工作得非常好。但在XCode 4.5上,它旋转了90°(四分之一圈),屏幕右侧有一个空白区域(我的视图大小正确,但超出了屏幕)。看起来是这样的: - (void)applicationDidFinishLaunching:(UIApplication *)application { NSLog(@"applicationDidFinishLaunching"); self
- (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];
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
我查看了以下帖子,但没有帮助:
有人有这个问题吗
任何帮助都将不胜感激 确保您也在设置window.rootViewController。我也有同样的问题,但这一行为我解决了:
MainViewController *mainView = [[MainViewController alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = mainView;
我升级的一个应用程序也有类似的问题。我还没有发现它的文件,但似乎有一个变化。我最终发现,新窗口似乎不知道旋转或大小,直到它成为关键和可见。我能够在makeKeyAndVisible之后立即将帧设置移动到,一切正常。我希望这对您有所帮助。谢谢大家的回复。我终于找到了解决办法 首先,在目标摘要菜单(项目的蓝色图标=>target=>summary=>底部滚动)中检查所有启动图像的方向和大小是否正确;如果大小或方向不正确,则会在未正确设置的启动图像上显示警告 到目前为止,我有一个采用这种结构(旧XCode方式)的项目:
和.mAppDelegate.h
和.mRootViewController.h
- a
和MainWindow Iphone.xib
(使用Interface Builder中链接的MainWindow iPad.xib
;请参见下面的屏幕截图,其中带有相对于RootViewController
的黄色/棕色图标(内部带有正方形)RootViewController
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:[rootViewController view]];
[window makeKeyAndVisible];
}
我所做的是更接近使用XCode 4.5创建空项目时得到的结构。因此:
MainWindow.xib
和MainWindow.xib
,现在以编程方式创建了我的窗口(更清晰、更好,以确保它适合任何屏幕的大小)Info.plist
中定义的“主nib文件库名称”和“主nib文件库名称(iPad)”键(并设置为MainWindow.xib
和MainWindow iPad.xib
)RootViewController\u iPhone.xib
和RootViewController\u iPad.xib
ApplicationIDFinishLaunching
方法中更改了如下代码:
- (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];
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
顺便说一句,要确保所有视图都是全屏的(在iPhone 5上),请确保在Interface Builder中将视图设置为“缩放填充”模式,并单击“自动调整子视图大小”。如果某些视图未缩放到全屏,可能是由于您创建控制器/视图的顺序(只有在创建超级视图时,超级视图才会向其子视图发送通知)。要轻松解决此问题,只需在-(void)viewDidLoad方法中添加以下代码即可:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
[self.view setFrame:CGRectMake(0,0,screenBounds.size.width,screenBounds.size.height)];
或使用:
[self presentModalViewController:myViewController animated:TRUE];
而不是:
[self.view.superview addSubview:myViewController.view];
presentModalViewController
确实会向子视图发送调整大小通知
希望这会有帮助 如果您使用的是故事板,一个简单的技巧就是使用loadView方法。在viewDidLoad之前调用它。只需转到情节提要并删除关联的视图,因为您将在loadView中以编程方式创建它。然后返回视图控制器类并复制以下代码:
[[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)];
- (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;
}
按顺序执行几个简单的步骤将为您解决此问题
首先,在AppDelegate.m中,检查是否将rootViewController添加为子视图。就是不是这个,
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
做这样的事
- (void)applicationDidFinishLaunching:(UIApplication *)application {
window.rootViewController = navigationController;
[window makeKeyAndVisible];
}
如果要将导航控制器设置为根视图控制器
其次,如果需要在navigationController的推式ViewController中控制旋转方法,请为UINavigationController创建一个类别,如下所示:
#import "UINavigationController+Rotation.h"
@implementation UINavigationController (Rotation)
- (BOOL)shouldAutorotate {
BOOL result = self.topViewController.shouldAutorotate;
return result;
}
- (NSUInteger)supportedInterfaceOrientations {
return self.topViewController.supportedInterfaceOrientations;
}
@end
现在,这两种面向iOS 6的定向方法
-(BOOL)应自动旋转和
-(NSInteger)支持接口方向
会在你的课堂上被调用
这是必要的,因为较旧的旋转方法,例如
-(BOOL)应自动旋转指针面定向:(ui界面定向)界面定向
已从iOS 6中弃用
最后,您需要在视图控制器中实现这些功能;大概是这样的:
- (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];
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
如果要允许视图控制器旋转并支持所有方向
其他方向遮罩包括:
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape和
UIInterfaceOrientationMaskAllButUpsideDown
在您的info.plist.in xcode中检查iOS 5.1模拟器支持的方向,并检查它是否正常工作,如果是,则需要执行一些代码以使应用程序与iOS 6兼容,并请粘贴用于方向的代码……是的,该应用程序在iOS 5.1 iPad模拟器上运行良好。该问题发生在iOS 6 iPad模拟器上(与设备上的旋转问题相同)。我想我将从一个空项目重新启动,并转移我的代码,并可能重新创建xib。。。将有一些代码作为前一个代码编写给ma