how can i do landscape disable for just one viewController iOS - ios

my project have UITabbar, UınavigationBar and UIViewContoller.
-UITabbar
—UINavigationController
—UIViewController
Question: how can i do landscape disable just one viewController? i want to only portrait view but for just one view.

disable autorotate on a single UIViewController in iOS6
Add this to your app delegate
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[window.rootViewController presentedViewController] isKindOfClass:[YourViewController class]])
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAllButUpsideDown;
}
EDIT 2
I recently found out about this, and it has been working perfectly for me. Add this code to your view controller
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
And in your viewWillAppear add
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
///Disable orientation changes
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}

Related

iOS with NavigationController As Root Make, One Controller ONLY Landscape

I have an app in which the root controller is a UINavigationController. Push one button, and it pushes another view controller. Push yet another and it starts an AVCaptureSession with Overlay. I want THAT view, and that view ONLY to be Landscape, and not portrait. I have looked at a ton of different tutorials, and nothing is working.
The closest I have gotten is:
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
in the viewDidAppear. However, there are some issues. One, it immediately goes right back to Portrait. Two, the other views are still all able to be portrait and landscape and I don't want that. I need help, please.
In my app delegate, I use this code and make sure only portrait mode is selected:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass:[SecondViewController class]])
{
SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;
if (secondController.isPresented)
{
return UIInterfaceOrientationMaskLandscape;
}
else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}
yes you can make particular viewController as a landscape by placing bellow method in your appDelegate.
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
then take one bool property in your appDelegate
#property (nonatomic) BOOL restrictRotation;
and then in view controller where you want to change orientation create appDelegate shared instance and allow restriction to change like bellow method
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
now call this method from your view controller and change orientation as you want.
[self restrictRotation:NO];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
and the most important thing is to change orientation to portrait when your view will be disappear otherwise all the other viewController also become in a landscape mode.
-(void)viewWillDisappear:(BOOL)animated
{
NSNumber *value = [NSNumber numberWithInt:UIDeviceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[self restrictRotation:YES];
}
I hope this will help you.

How to force show MasterViewController in Portrait mode on iPad using UISplitViewController iOS

I'm making the app with UISplitViewController but the it require show only the MasterViewController on Portrait mode in Ipad (work same as the Portrait mode on iPhone). In the landscape mode, it works normally.
Please give me some idea to make it. Thanks in advance.
Put this in viewWillAppear
NSNumber *value = [NSNumber numberWithInt: UIInterfaceOrientationMaskPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
plz use this code in app delegate class
-(id)getTopViewController{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
if (![topController isKindOfClass:[NSNull class]]) {
return topController;
}
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
UIViewController *viewOb = [self getTopViewController];
if ([viewOb isKindOfClass:[MasterViewController class]]) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAll;
}
Plese use this code it will be help you out
- (BOOL)shouldAutorotate{
//disable rotation
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//allow only portrait interface orientation
return UIInterfaceOrientationMaskPortrait;
}

How to force my view controller to always open in portrait mode

I have two view controllers embedded in a navigation controller, My first view controller is allowed to rotate whereas my second view controller should always open in portrait mode, For instance even if I am in landscape mode in first view controller I want to open my second in portrait only,
I present the second view controller by pushing segue from first.
You will have to implement shouldAutorotate on the second VC
Just before you present the second VC (portrait) call
if([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
}
You can use this method in ur AppDelegate Class for and maintain with bool var
-(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if(self.isDisplayInPoratrait){
return UIInterfaceOrientationMaskPortrait;
else
// return ur required orientation
}
self.isDisplayInPoratrait is bool variable declared in AppDelagate set this variable yes in class which u want to present in portrait. Place this method in class which u wan to present in portrait
-(BOOL)shouldAutorotate {
return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
Solution 1 : create a custom UINavigationController subclass and override these methods like this :
#interface LockedNavigationViewController ()
#end
#implementation LockedNavigationViewController
-(BOOL)shouldAutorotate {
return UIInterfaceOrientationMaskPortrait;
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
#end
Select your project - > general, then follow the above image.

Xcode IOS Lock Some orientation to Portrait and Some to landscape

I am working on an application that has about 12 views. I want some to be portrait only and others to be landscape.
How would I go about locking some view's orientation to portrait and others to landscape?
I tried the code below from a previous example, but It does not lock the orientation.
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
forKey:#"orientation"];
After almost 24 hours of trying I finally fixed the problem. For one "ShouldAutoRotate" never gets called. And thats all I could find everywhere "Just add shouldAutoRotate"... Didnt work. Also i am not sure why people are down voting my question.
What work for me was the add the method below to my Appdelegate.m file.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
Then add the method below to the viewcontroller.m file I want to be locked to landscape.
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
You have to implement supportedInterfaceOrientation and shouldAutorotate in your ViewController.
Apple Documentation

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;
}

Resources