1

I want to change the orientation of the app without changing the device orientation in iphone app. I want to change my view from portraid mode to landscap mode programmatically.

And also want to know that will this be accepted by the apple store or not ?

Thanks

Now I got the solution from other that is as follow

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

when you add this line at that time one warning appear and for remove this warning just add bellow code on you implementation file..

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)


    - (void) setOrientation:(UIInterfaceOrientation)orientation;


    @end

and after that in bellow method just write this code if required..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    {

        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    }

But now want to know is this accepted by apple app store or not ?

thanks

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
uttam
  • 1,364
  • 4
  • 20
  • 35

5 Answers5

5

use this line for programmatically change orientation...

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

and also when you add this line at that time one warning appear and for remove this warning just add bellow code on you implementation file..

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end

and after that in bellow method just write this code if required..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //    return NO;
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

i hope this help you..

:)

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • nothing change with your code,getting no change in UI.the UI should change from portrait to landscap – uttam Sep 27 '12 at 06:38
  • just put above this line in you viewWillAppear: method .. [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; – Paras Joshi Sep 27 '12 at 06:41
  • I want orientation change on click of button. and i am having shouldAutorotatetoInterFaceorientation retuns yes. – uttam Sep 27 '12 at 06:43
  • I just got it working..... :-) now just want to know is it acceptable by apple app store ? thanks you – uttam Sep 27 '12 at 06:46
  • most wel-come mate.. but now i am not sure that this accepted by apple or not but for setOrientation method it just give the warning if above code not implemented.. – Paras Joshi Sep 27 '12 at 06:52
  • I have just updated my ios 5 to ios 6 and now it stopped working.how can i solve this. – uttam Sep 28 '12 at 12:27
3

Add a class variable

Bool isInLandsCapeOrientation;

in viewDidLoad

set this flag to

isInLandsCapeOrientation = false;

Add the following function

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     if (!isInLandsCapeOrientation) {
         return (UIInterfaceOrientationIsPortrait(interfaceOrientation));

     }else {
         return  (UIInterfaceOrientationIsLandscape(interfaceOrientation));
     }
}

To changing orientation from portrait to landscape, let it happens on a button action

 - (IBAction)changeOrientationButtonPressed:(UIButton *)sender
{
    isInLandsCapeOrientation = true;
    UIViewController *viewController = [[UIViewController alloc] init];
    [self presentModalViewController:viewController animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}

This works fine for me.

Rashid
  • 829
  • 7
  • 6
1

To change Orientation portraid mode to landscap mode use this code

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

use this code for programmatically change orientation...

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
kunalg
  • 1,570
  • 1
  • 12
  • 18
0

The documentation describes the orientation property as being read-only, so if it works, I'm not sure you can rely on it working in the future (unless Apple does the smart thing and changes this; forcing orientation changes, regardless of how the user is currently holding their device, is such an obvious functional need).

As an alternative, the following code inserted in viewDidLoad will successfully (and somewhat curiously) force orientation (assuming you've already modified you shouldAutorotateToInterfaceOrientation ):

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}

Clearly, this does it if the user is currently holding their device in portrait orientation (and thus presumably your shouldAutorotateToInterfaceOrientation is set up for landscape only and this routine will shift it to landscape if the user's holding their device in portrait mode). You'd simply swap the UIDeviceOrientationIsPortrait with UIDeviceOrientationIsLandscape if your shouldAutorotateToInterfaceOirentation is set up for portrait only.

For some reason, removing the view from the main window and then re-adding it forces it to query shouldAutorotateToInterfaceOrientation and set the orientation correctly. Given that this isn't an Apple approved approach, maybe one should refrain from using it, but it works for me. Your mileage may vary. But this also refers to other techniques, too. Check SO discussion

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
0

If you want to change the particular view only in landscape..then u can try the following in its viewDidLoad

    float   angle = M_PI / 2; 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
   [ [self.view] setTransform:transform];
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27