0

I'm developing an iPad app that needs to be restricted to landscape mode. I've got a root view controller and a subview controller. I add the sub view controller to the root view controller and set their frames to 1024x768. I restricted the app to landscape mode in info.plist and in my view controllers' shouldAutorotateToInterfaceOrientation:interfaceOrientation method. However, the dimensions for the subview get reset to 768x1024 sometime before the viewDidAppear method which makes it appear as if it's in portrait mode.

The strange part is if I set the frame for the subview so that the width or height are something other than the screen dimensions, for example: CGRectMake(0, 0, 1024, 767) or CGRectMake(0, 0, 1024, 769), it doesn't get reset and displays in what looks like landscape mode.

So my question is, what's causing this behavior. And what's the best way to simply default all views to landscape mode

Here's my root view controller:

// 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

Here's my sub view controller

// 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
moshe
  • 157
  • 10

1 Answers1

0

if you uses xib files then just make your subview's xib to represent it as landscape by default ie the default design of nib file will be landscap ie make your subview's view landscap in property window

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
  • Hi Aalok. In my quest for a landscaped subview I also tried creating a .xib to correspond to the sub view controller in which it was set to landscape. I instantiated it with `initWIthNibName:bundle` and it achieved the same effect. When it was set to 1024x768 the width/height were swapped but when it was 1024x769 it worked. I got it to work by resetting the frame in sub view controller's `viewDidAppear` method, but this seems like it should be unnecessary. I'm stymied. – moshe May 02 '12 at 16:52