Question

I have app for iPhone and it's done. This works only for potrait mode.

Client need an update saying whenever user browse photo/ watch video, he need landscape mode too so that when he rotate image or video, it will get full screen.

For this I ticked all four Device Orientation.

enter image description here

Then based on this answer, I made custom navigation controller file and assigned it.

.h file

#import <UIKit/UIKit.h>

@interface CustomNavigationController : UINavigationController

@end

.m file

#import "CustomNavigationController.h"

@interface CustomNavigationController ()

@end


@implementation CustomNavigationController

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


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

@end

Now when I run app and rotate to landscape, still I see potrait mode only.

Till now this is fine.

Now what I want to do is, for some screens, I want landscape mode too. Any idea how can I do the same?


Edit 2

When I hide what I have in CustomNavigationController, and try to rotate iPhone, the image is also rotating. This is happening because I have hidden the code which says "I am not allowing for rotation" return NO.

So is there any way where I can say for the images, also allow iphone to go to landscape mode?

For photo browsing, I am using https://github.com/mwaterfall/MWPhotoBrowser

Was it helpful?

Solution

When we are talking about orientation, they are 2 things that come into the picture:

  1. Device Orientation
  2. Interface Orientation

As its clear by the name only, Device orientation tells, in which orientation device is, and Interface orientation says in which orientation your app is presenting its interface.

"Client need an update saying whenever user browse photo/ watch video, he need landscape mode too so that when he rotate image or video, it will get full screen."

By this, its clear that you don't need to change interface of your App. What you need is to rotate the image, when device orientation changes.

For that, First thing you should do is, have a look at this method beginGeneratingDeviceOrientationNotifications

Write this to get notified each time when Device orientation is changed [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Now, in the viewController where you are showing the images/videos, write this

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];

In deviceOrientationDidChange method, rotate the image as per device orientation Let me know if more info needed..

Edit: There are 4 orientations possible:

  • UIInterfaceOrientationLandscapeLeft
  • UIInterfaceOrientationPortrait
  • UIInterfaceOrientationLandscapeRight &
  • UIInterfaceOrientationPortraitUpsideDown

As you said you are using MPMoviePlayerController, that supports Landscape Mode. Your code should not depend on which orientation the player is supporting, which its not. You should write code in such a way that it should support all the orientation.

So, as I said earlier, In deviceOrientationDidChange method, rotate the image as per device orientation.

Let me know if still some issues are there.

EDIT 2: First of all uncheck all orientation support (Except portrait one). Now you don't have to change interface orientation in your code. What you have to do is, you have to detect device orientation change & rotate image like this:

myImage.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);

I don't think you need customNavigationController to do that. Just try the steps I told you with default NavigationController. It's working fine for me for similar app. Try it with a demo application & then do necessary updates in your main App. Still, Let me know if you get stuck somewhere. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top