elez
elez

Reputation: 55

code which support landscape and portrait in iPhone

How can I write my code which supports both landscape and portrait? I tried this one -

(void)setupForOrientation:(UIInterfaceOrientation)orientation {
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        CGRect bounds = CGRectMake(0, 0, 768, 1004);
        scrollView.frame = bounds;
        // Other view positioning for portrait.
    } else {
        CGRect bounds = CGRectMake(0, 0, 1024, 748);
        scrollView.frame = bounds;
        // Other view positioning for landscape.
    }
    [self drawBackgroundForOrientation:orientation];
}

But its not working. Please help me.

Upvotes: 1

Views: 577

Answers (4)

rohan-patel
rohan-patel

Reputation: 5782

Use this code:

- (void)viewDidLoad

    {

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

    }


- (void) orientationChanged:(id)object
{  


 if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight )
 {
 self.view=self.landscapeView;
 NSLog(@"ViewwillAppear= Land");

 }
 else
 {
 self.view = self.portraitView;
 NSLog(@"ViewwillAppear= Port");

 }

You'll always have to return yes for this function whenever you require orientation support.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
return YES;
}

Edit 1: P.S. I've taken portraitView & landscapeView as two separate UIViews.

Upvotes: 2

Rahul Sharma
Rahul Sharma

Reputation: 3013

If you have used viewcontrollers then there are a few methods that you can override. The first being

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; // Override to allow rotation. Default returns YES only for UIInterfaceOrientationPortrait

Where you can specify whether a particular view will support rotation or not.

If you need to configure your views manually according to the orientation you can override the following methods

As per the documentation

Your implementation of this method should simply return YES or NO based on the value in the interfaceOrientation parameter. Do not attempt to get the value of the interfaceOrientation property or check the orientation value reported by the UIDevice class. Your view controller is either capable of supporting a given orientation or it is not.

// Notifies when rotation begins, reaches halfway point and ends.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

AND

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

To achieve what you wish to implement you can return YES for supported orientation and then use the second method to configure your view accordingly by calling [self setupForOrientation:orientation];

Hope it helps!!!

Upvotes: 0

Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

You have to use following method to support both orientation

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
        return YES;
}

//this method will call just before changing Orientation of device you can set frames in this method

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
}

Upvotes: 0

Ian
Ian

Reputation: 3836

I think you want to override the shouldAutorotateToInterfaceOrientation: method of your main view controller to always return YES. That or you can specify the supported orientations for your app in the app properties.

Upvotes: 1

Related Questions