How to use MPMoviePlayerController to play video in landscape orientation in iOS all across the Application. I am using the MPMoviePlayerController in many viewControllers of my app and I want all the MKMoviePlayerController torun only in landscape mode irrespective of the orientation of the parent view controller from where this ViewController is called.
Enable landscape orientation in your project settings.
Subclass UIViewController and in your new subclass (e.g. MyViewController) add these callbacks:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Make all your view controllers of type MyViewController.
In the views you want to force landscape override those methods with:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Related
i have a tabbar ios app. i want to make one of the tabs portrait only. how do i do that? i already tried the solution in How to force view controller orientation in iOS 8? and didn't help. I put that in view controller's .m code. This view controller has both a UIView and UITableView in it. pl see image below;
thank you.
in your all viewcontroler write this code:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll; //allow rotate landscape, portrait
}
and write this code in in which viewconroller you want only portrait.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown; // portrait only
}
OR you can use this one:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass: [CompareViewController class]])
{
CompareViewController *compareController = (CompareViewController *) self.window.rootViewController.presentedViewController;
if (compareController.isPresented)
return UIInterfaceOrientationMaskLandscape;
else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}
Try to create extension of UITabbarController, and then override below 2 methods in extension:
supportedInterfaceOrientations
shouldAutorotate
And in both the methods:
just validate,
if self.selectedIndex = 'particular index' (the tab index, which you want portrait) and return accordingly.
There is My ViewController "MyViewControllA" which support both Orientation Landscape and Portrait . And my ViewController "MyViewControllB" supports only Portrait Mode .And I am Pushing "MyViewControllB" on "MyViewControllA" Now it Works fine when "MyViewControllA" is in Portrait Mode . But When "MyViewControllA" is in Lanscape mode The UI of "MyViewControllB" gets Distorted .
I am Using these two methods in "MyViewControllB" for Orientation
- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate{
return UIInterfaceOrientationMaskPortrait;
}
Please correct me where I am wrong???
Thanks
How the orientation is decided using containers controller basically depends on your view controllers hierarchy.
If I remember well the navigation controller supports only the orientation that is common to all your view controllers. To override this behavior you can subclass the UINavigationController class or (better) use its protocol.
navigationControllerPreferredInterfaceOrientationForPresentation:
navigationControllerSupportedInterfaceOrientations:
If you use an NavigationController you have to implement something like that:
- (BOOL)shouldAutorotate {
return [self.visibleViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
return [self.visibleViewController supportedInterfaceOrientations];
}
So the function call in every ViewController is visible.
I'm utilizing a navigation controller in my app. I want all of my viewController to be in portrait only except for one, which will support landscape.
Going with an accepted answer in Stack Overflow I subclassed my nav controller and include this code:
iOS 6 - Navigation Controller Landscape Rotations For Some Views While Others Portrait Only
- (BOOL)shouldAutorotate {
id currentViewController = self.topViewController;
if ([currentViewController isKindOfClass:[IssueViewController class]])
return YES;
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
This singles out the one view controller I would like to rotate. Everything works great except when I launch the app in landscape. The image gets crushed and off centered.
screen shots here: http://imgur.com/a/jyrJ2
Any suggestions on correcting this? Thank you!
Tested your code. This works fine only when you change the orientation, but it doesn't reflect changes according to current interface orientation if you launch the app in landscape or go from one to other controller in landscape.
Use the following approach, which I have used to solve same kind of issue some time ago.
Add this code in UINavigationController subclass
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
And in you ViewController, add this code
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
//Control UIInterfaceOrientationMask here to tell which interface orientations you want to support for this ViewController
return UIInterfaceOrientationMaskPortrait;
}
I have five view controllers and In Project Target Device Orientation I have enabled Portrait ,Landscape Left and Landscape Right. Now I want 4 view controllers out of 5 view controllers Stay in Portrait mode (Not Rotate into landscape left and landscape right) and only one view controller rotate in all modes(Portrait,landscape left,landscape right). So How can do this please tell .
Implement -(NSUInteger)supportedInterfaceOrientations for each of your ViewControllers and specify which interface orientations each controller should support.
Edit
Assuming that you have a separate implementation for each of your ViewControllers, implement -(NSUInteger)supportedInterfaceOrientations and -(BOOL)shouldAutorotate in each of your implementations.
For example
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
will ensure that your view controller supports all landscape modes. Combine this with
-(BOOL)shouldAutorotate{
return YES;
}
and your display will "flip" over when rotated.
Use the enum UIInterfaceOrientationMask to adjust which orientations are supported and try different combinations of this along with a YES/NO return value to -(BOOL)shouldAutorotate until you get the behaviour you want.
First of all, in AppDelegate, write this.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
For UIViewControllers, which require LANDSCAPE too, change masking to All.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
//OR return UIInterfaceOrientationMaskAll;
}
Now, if you want to do some changes when Orientation changes, then use this function.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
NOTE Please :-
A lot depends on with which controller is your UIViewController embedded in.
Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.
subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) did set it as self.window.rootViewController.
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.
If I have AViewController which is a subclass of TabBarController and NavigationController, is it possible to set it to portrait? while, the other ViewController can rotate normally. (Portrait and Landscape)
Thank you
Yes it is. You can find the documentation here. You just have to override a few methods and return supported orientations for each view controller, such as supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods.
For example, to limit the orientation to portrait you would do
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
Edit:
I added the preferredInterfaceOrientationForPresentation method. Try this. This is the exact code i'm using in one of our apps, and it is working for us.