Return to portrait after landscape view - ios

I have an application that need to be on portrait orientation for all view, and one view (UIWebview) that need to support portrait and lansdscape orietation.
To set the default portrait orientation , I put this in my app delegate:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//NSLog(#"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
if(flagOrientationAll == YES){
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskPortrait;
}}
And I have manage to arrange my webview to portrait/landscape regarding the user orientation using this method (got this from stackoverflow also)
-(void)viewWillAppear:(BOOL)animated
{
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
//NSLog(#"viewWillDisappear -- Start");
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = NO;
}
And to present portrait and landscape i using two UIView:
-(void)viewWillLayoutSubviews {
BOOL layoutIsPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
if (layoutIsPortrait) {
self.view = self.pView;
[self.pView addSubview:pwebView];
}else{
self.view = self.lView;
[self.lView addSubview:lwebView];
}}
Then to dismiss the webview i using this code:
- (IBAction)doneButtonTapped:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
However when it dismiss in landscape state, the previous screen (the screen that call the webview) present in landscape instead of the default(portrait).
How i can make after the webview dismiss it will still present in portrait?
thanks

You should implement the supported orientation for each viewController:
Like i wrote here:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
//Choose your available orientation, you can also support more tipe using the symbol |
//e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
return (UIInterfaceOrientationMaskPortrait);
}

Swift 3.0:
(Added to the ViewController that calls the landscape-able view)
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}

Related

after launch in landscape cannot force view to start in portrait

I know that this subject has been asked before, but I went over all solutions and none solved the case I'm having.
My app is all in portrait format, only one view controller in landscape format.
I added to all view controllers the following code to force Portrait Orientation regardless the device orientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(void)ViewDidLoad()
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}
If the device is set in portrait mode the viewController opens correctly, but if the device is set landscape mode before opening the app, then the app launches the splash screen in landscape and opens the view controller in landscape with part of the view clipped, then it rotates it to portrait mode, and keeps part of the screen clipped, as shown in the picture.
in the app delegate:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
And in the appDelegate also, just before adding the view controller to the navigation controller I added the following:
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:#"orientation"];
This seems to be a new thing since iOS 11. I fixed it by allowing autorotate but only supporting portrait.
(BOOL)shouldAutorotate{
return YES;
}
(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
I'm having the same problem. But I fix it by rotate the device orientation programmatically.
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if UIDevice.current.orientation != .portrait {
UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")
}
return .portrait
}
ok here what I did and it finally working well, hope it can help someone:
in appDelegate I'm putting the following:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
To introduce the main view I'm using the following from appDelegate:
self.navigationController = [[UINavigationControllerSub alloc] initWithRootViewController:libraryVC];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
portraitSize = CGSizeMake(navigationController.view.frame.size.width, navigationController.view.frame.size.height);
[self.window setRootViewController:navigationController];
In the main view which I launched from the app delegate and I need it to be only portrait I'm doing the following:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:#"orientation"];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:NO];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}
When I want to launch a view where I need it to be landscape I use the following just before launching:
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES];
[UIApplication sharedApplication].statusBarOrientation = UIDeviceOrientationLandscapeLeft;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:#"orientation"];
You need to set your project as Portrait only to make sure the app launches in portrait regardless of the device's orientation.
Also, make sure you have supportedInterfaceOrientationsForWindow implemented on your AppDelegate and that it returns UIInterfaceOrientationMaskAll or whatever mask makes sense for your project.
With all that, you'll have the app launching with portrait and control over allowed orientations per view controller, with the implementation you already have.

How to show one view controller in landscape mode?

HI I'm using below code for Landscape Mode for only one view controller
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight; // or Right of course
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
It is working fine But when i rotate to Portrait Then it is rotating and i'm using below code for presenting the view
[self presentViewController:vc animated:YES completion:nil];
where I'm Missing Thanks in advance
Use this code
In your AppDelegate.h
#property (assign, nonatomic) BOOL shouldRotate;
AppDelegate.m
BOOL shouldRotate;
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (self.shouldRotate)
return UIInterfaceOrientationMaskAllButUpsideDown;
else
return UIInterfaceOrientationMaskPortrait;
}
Now open your ViewController in which you want to apply orientations and use this code
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES]; // set NO to disable rotation

change one view orientation to landscape

I'm working on a project in which user will watch live channels. But I'm facing a small problem here and I've tried very hard but failed to find any solution. My app will support portrait orientation for all views but last one. Last view will support only landscape orientation. Is it possible?
I've searched and tried following code
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
//
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
//
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);}
Follow this link. Hope you get your answer here.
The link shows how to keep all your views in portrait mode except one view that will be in landscape.
You need to do the following:
1st :
Implement this in all controllers that are fix for portrait:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
And implement this for the landscape controllers:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
2nd :
// Fetch the status bar from the app and set its orientation as required.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
// Create an empty view controller, present it modally and remove it directly afterwards
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
// Now the device is rotated to the desired orientation. Go from there.
Hope that works.
From iOS6 apple has changed orientation update mechanism. From iOS6 onwards, iOS will report orientation event only to RootController, so all the orientation related decision can only be taken in RootController. Lets say you are using UINavigationController or UITabBarController as window's root controller. In this case you can create sub class of UINavigationController or UITabBarController, override orientation related methods and pass orientation related events to childControllers. Set this custom UINavigationController or UITabBarController object as rootController of your window.
You can override below methods in your custom class.
-(BOOL)shouldAutorotate
{
BOOL shouldRotate = YES;
if(self.viewControllers.count > 0)
shouldRotate = [[self.viewControllers lastObject] shouldAutorotate];
return shouldRotate;
}
-(NSUInteger)supportedInterfaceOrientations
{
NSUInteger supportedInterfaces = UIInterfaceOrientationMaskAll;
if(self.viewControllers.count > 0)
supportedInterfaces = [[self.viewControllers lastObject] supportedInterfaceOrientations];
return supportedInterfaces;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
UIInterfaceOrientation preferredOrientation = UIInterfaceOrientationPortrait;
if(self.viewControllers.count > 0)
preferredOrientation = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
return preferredOrientation;
}
Use Following approach : In your app delegate .h
#interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
BOOL flagOrientationAll;
}
#property (assign) BOOL flagOrientationAll;
Add following method in your app delegate .m file
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//NSLog(#"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
if(flagOrientationAll == YES){
return UIInterfaceOrientationMaskLandscapeRight;
} else {
return UIInterfaceOrientationMaskPortrait ; // your Default orientation for all other view
}
}
Implement following way in your view which you want to rotate in both portrait and landscape both for iPhone device
-(void)viewWillAppear:(BOOL)animated
{
self.tabBarController.delegate = self;
PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = YES;
}
}
-(void)viewWillDisappear:(BOOL)animated
{
//NSLog(#"viewWillDisappear -- Start");
PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = NO;
}

Orientation doesn't change after Pushing (push segue) a View on the UINavigationViewController

I have embedded a UIViewController in a UINavigationController. The orientation of the view of this controller is set to Portait. When I push a new view on this UIViewController, which is landscape only, the new view is being shown portrait as well, instead of it's orientation landscape.
I have tried to subclass the UINavigationController and added the following methods like this:
- (BOOL)shouldAutorotate {
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations {
return self.topViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return self.topViewController.preferredInterfaceOrientationForPresentation;
}
In the rootViewController (LoginViewController) I did this:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return YES;
}
In the pushViewController (A custom ViewController) I did this:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}
I'm using a storyboard and a push segue between them. I know that the problem lies in the push segue which leads to an taking over of the orientation of the topviewcontroller which is portrait and the pushViewController is landscape. Does anyboy know workarounds?
Any help is thankfully appreciated. Else I should drop the navVC and perform a modal segue.
KR
Try this code :
In AppDelegate.m class write below code.
#pragma mark Orientation Code
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientations = UIInterfaceOrientationMaskAll;
if (self.window.rootViewController) {
UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presented supportedInterfaceOrientations];
}
return orientations;
}
And next if you don't want orientation of the particular class for example
Stop orientation viewController.m
#pragma mark Orientation
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
final thing to change project device orientation of project Target.
Ex : Project TARGETS --> Device Orientation -- > select All (Portrait, UpSide Down, Landscape Left, Landscape Right)

Orientation change issue in iOS6

My application is working fine for iOS5. But for iOS6 I am getting the following orientation problem.
I have a ViewController VC1. when you rotate (change the orientation to UIInterfaceOrientationLandscapeRight) I want to present another ViewController VC2 and when you rotate back I need to dismiss VC2 and the VC1 should be in Portrait mode.
I am using tabBar in my application and I want this feature only for the first tab.
In tabBar I have written
-(BOOL) shouldAutorotate
{
UINavigationController *nav = (UINavigationController *)self.selectedViewController;
if ([nav.topViewController isKindOfClass:[MainViewController class]])
{
return YES;
}
else
{
return NO;
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//Here I am writing code for presenting view(using notifications)
// but When I rotate the device to landscape it's getting called but when I rotate back
//to portrait I not getting called.
}
Thank you.
Please try this with ios 6 :(Example)
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}

Resources