0

I want to disable the autorotation of iOS device for few view controllers.Like i have one few controller i want it to be displayed only in portrait mode.Where as others view controller in landscape mode.I have used following code but these delegate methods are never called?

#pragma mark Orientation handling

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return  (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait);
}
TechChain
  • 8,404
  • 29
  • 103
  • 228
  • Hope this helps you http://stackoverflow.com/questions/31158690/force-the-ios-device-to-change-orientation/31158872#31158872 – soumya Jul 10 '15 at 11:34
  • Are you inside a navigation controller? If so, you should override thoses methods in your navigation controller. – Jaeger Jul 10 '15 at 11:36
  • I am not inside navigation controller – TechChain Jul 10 '15 at 11:39
  • Those *aren't* delegate messages. Which class have you put them into and what's your complete view controller hierarchy? – Tommy Jul 10 '15 at 12:12

1 Answers1

3

I used following method to forcefully set portrait orientation only for some selected view and its work for me, may be it will be help you

1, Create one global flag

Create on Global flag in your AppDelegate Method So you can access it in any View controllers .

In your AppDelegate.h

@property (assign) BOOL flagOrientationAll;

In your AppDelegate.m

@synthesize flagOrientationAll;

Add following method in your AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if(flagOrientationAll == YES){
        return UIInterfaceOrientationMaskAll;  // All orientation support
    } else {
        return UIInterfaceOrientationMaskPortrait;   // Set only one orientation you want 
    }
}

2, Add following code in ViewController .m for that you want to restrict rotation

// set flag "flagOrientationAll" to rotate only one view in your particular view 

#import AppDelegate.h
-(void)viewWillAppear:(BOOL)animated
{
    NSLog (@"webViewController -- viewWillAppear");
    [super viewWillAppear:animated];

    AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    delegate.flagOrientationAll = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
   AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    delegate.flagOrientationAll = NO;
}

Here my POST : How to set one of the screens in landscape mode in iphone?

If you get any trouble let me know!!

Community
  • 1
  • 1
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45