如何将根视图控制器默认为横向,大小为1024x768

时间:2012-05-02 12:14:02

标签: ipad uiviewcontroller landscape

我正在开发一款需要限制为横向模式的iPad应用。我有一个根视图控制器 和一个子视图控制器。我将子视图控制器添加到根视图控制器并设置它们 帧到1024x768。我将应用程序限制在info.plist和我的视图控制器中的横向模式 shouldAutorotateToInterfaceOrientation:interfaceOrientation方法。但是,尺寸为 子视图在viewDidAppear方法之前的某个时间重置为768x1024 好像它处于纵向模式。

奇怪的部分是我为子视图设置框架,以便宽度或高度是某种东西 除了屏幕尺寸,例如: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

1 个答案:

答案 0 :(得分:0)

如果您使用xib文件,那么只需将您的子视图的xib默认表示为横向,即nib文件的默认设计将是landscap,即在属性窗口中显示您的子视图的视图