我正在开发一个需要限制为横向模式的 iPad 应用程序。我有一个根视图控制器和一个子视图控制器。我将子视图控制器添加到根视图控制器并将它们的帧设置为 1024x768。我在 info.plist 和我的视图控制器
shouldAutorotateToInterfaceOrientation:interfaceOrientation
方法中将应用程序限制为横向模式。但是,子视图的尺寸会在方法之前的某个时间重置为 768x1024,viewDidAppear
这使它看起来好像处于纵向模式。
奇怪的是,如果我为子视图设置框架,以便宽度或高度不是屏幕尺寸,例如:CGRectMake(0, 0, 1024, 767) 或 CGRectMake(0, 0, 1024, 769) ,它不会被重置并以横向模式显示。
所以我的问题是,是什么导致了这种行为。什么是简单地将所有视图默认为横向模式的最佳方法
这是我的根视图控制器:
// INTERFACE
#import <UIKit/UIKit.h>
#import "ScrapViewController.h"
@interface ViewController : UIViewController
{
ScrapViewController *svc;
}
@end
// IMPLEMENTATION
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
self.view.frame = CGRectMake(0, 0, 1024, 768);
svc = [[SubViewController alloc] init];
svc.view.backgroundColor = [UIColor redColor];
svc.view.frame = CGRectMake(0, 0, 1024, 768);
[self.view addSubview:svc.view];
NSLog(@"viewDidLoad rvc %f,%f", self.view.frame.size.width, self.view.frame.size.height);
NSLog(@"viewDidLoad svc %f,%f", svc.view.frame.size.width, svc.view.frame.size.height);
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
@end
这是我的子视图控制器
// INTERFACE
#import <UIKit/UIKit.h>
@interface SubViewController : UIViewController
@end
// IMPLEMENTATION
@implementation SubViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
@end