Question

I have had this problem countless times and cannot figure out how to fix it. I am working in an Xcode project (empty project - NO XIB's!). I have my orientation set to landscape:

enter image description here

But this keeps happening:

enter image description here

The view is being cutoff. No matter what I do it doesn't seem to set to the proper size. For some reason it is displaying the view in landscape using portrait bounds. Does anyone know how to fix this? I also want to restrict the orientation to ONLY landscape.

UPDATE The view does not get cutoff if I hard-code 1024 as the width and 768 as the height. This is obviously a terrible solution, but I cannot figure it out. Does anyone out there know of a solution?

Was it helpful?

Solution 2

I have the answer! One of my friends helped me out with this one.

Views are not oriented until they appear, so, if you are going to add any components to the view and expect them to adhere to an orientation other than the default, which I suspect is portrait, you must add those components in the -(void)viewDidAppear:(BOOL)animated. I was calling methods that added several components to the view from within the viewDidLoad method, however, the view has not appeared, and the orientation not set, when that method is called. Moving my initialization code into the viewDidAppear method fixes my problem.

Here is an example:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidLoad];

    //Probably dont want to draw stuff in here, but if you did, it would adhere to the
    //correct orientation!
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.backgroundColor = [UIColor blueColor].CGColor;
    layer.anchorPoint = CGPointMake(0, 0);
    layer.bounds = CGRectMake(0, 0, self.view.bounds.size.width, 300);
    [self.view.layer addSublayer:layer];

    //Call methods from here
    [self initializeScrollView];
    [self addItemToScrollView];
    [self addGraphToView];
}  

OTHER TIPS

Check the rooViewController that you are setting in application:didFinishLaunchingWithOptions: in app delegate class. Make sure you are returning proper allowed orientations in this view controller class whose object you are setting to rootViewController:

- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationLandscapeRight;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

In your app delegate add this function :

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 

 return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationLandscapeRight; 
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top