0

I have started my project with iOS 5.0, Now updated to iOS 6, I am facing problem in doing orientation, To test I have created a sample application, and added the following code in delegate...

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAll;
}

And in my view controller I have implemented the following methods...

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

All the above I have done after googled about iOS 6.0,when I load application, based on the option given in supportedInterfaceOrientations view is loading that is fine, now when I changes my device orientations... view is not changing accordingly at run time, how do I change my view based on the device orientation?

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[notificationCenter addObserver:self
                       selector:@selector(deviceOrientationDidChange) 
                           name:UIDeviceOrientationDidChangeNotification object:nil];

Do I need to add observer like above and changes it programmatically? or Will the view change automatically by detecting device orientation?

Newbee
  • 3,231
  • 7
  • 42
  • 74

3 Answers3

4

try this

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}


- (BOOL) shouldAutorotate {
    return YES;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscape;
}

The previous code should work on iOS6

Anyways, I use this code on one of my projects. I have a boolean _firstTime, and I set it to YES before showing the viewController, then I change it to NO after the viewController appears

- (NSUInteger)supportedInterfaceOrientations {
    if (_firstTime) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskAll;
    }

}

- (BOOL) shouldAutorotate {

        return YES;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (_firstTime) {
        return return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    } else {
        return YES;
    }
}
jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • check the last update, I use it to remove the rotation while I show a video and it works – jcesarmobile Jan 02 '13 at 11:28
  • Yes, but your problem might be the UINavigationController, you should create a subclass of navigationController and put this code there. Then, create a custom navigation controllers instead the regular one – jcesarmobile Jan 02 '13 at 12:00
  • I have created sample (single view) application only, in which I have not used navigation controller self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; I have loaded like this.. ViewController view also not rotating, Any idea? – Newbee Jan 02 '13 at 12:10
  • can you upload the sample project somewhere? – jcesarmobile Jan 02 '13 at 12:21
  • iPad killed my time from morning I think, problem is with iPad, I tried with iPod now, working charm... Oh..god :) – Newbee Jan 02 '13 at 12:40
1

Change in these method:

-(NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskAll;
}

Note : No need to add observer for device notification.Given below method will specify orientation status

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

EDIT : Provide all your views will automasking according to requirement

EDIT : If using UINavigation Controller then its child View Controller's supportedInterfaceOrientations and shouldAutorotate method will not be called.

Refer this link.

Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • Still view is not rotating at run time. – Newbee Jan 02 '13 at 10:35
  • willRotateToInterfaceOrientation itself not getting called in my case? Any idea? what am doing wrong? – Newbee Jan 02 '13 at 10:49
  • I did already... all are selected... Before in iOS 5.0 no problem.. But in iOS 6.0 this is not working. And I'm using iPad is it matters? – Newbee Jan 02 '13 at 10:54
  • are u using UINavigation Controller? – Paresh Navadiya Jan 02 '13 at 11:08
  • Ya... self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navigationcontroller; [self.window makeKeyAndVisible]; ANYTHING WRONG IN THIS? – Newbee Jan 02 '13 at 11:11
  • 1
    that's issue here. Check edited answer.... http://stackoverflow.com/questions/12520030/how-to-force-a-uiviewcontroller-to-portait-orientation-in-ios-6 – Paresh Navadiya Jan 02 '13 at 11:14
0

Problem is with my iPad. it has a button to restrict auto rotation change... I was not aware initially, as I was worked fully with iPod.. Thanks all for your helps.

Newbee
  • 3,231
  • 7
  • 42
  • 74