iOS orientation change not working also UIDeviceOrientationDidChangeNotification not received

ios, iphone, notifications, orientation, rotation

Solution

See if you are setting the `self.window.rootViewController` in your `AppDelegate's` `didFinishLaunchingWithOptions`: method, because if you're adding only subviews to the window the orientation change notification does not fire.

Problem

I am quite new to iOS and I am trying desperately to get orientation changes working in my app. After researching here's what I do: In my app I have a NavigationController managing seven UIViewController subclasses. In the projects summary tab I activated all 4 device orientations. Each UIViewcontroller subclass has a xib file, all xib files have "autoresize subviews" activated. The UIViewController subclasses all have: ``` - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ); } ``` they also all have: ``` - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation ``` and: ``` - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration ``` implemented with an NSLog(...) statements (never printed, debugger also never entering these methods). Also I was trying to use: ``` [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]; NSLog(@"will receive orientation notifications: %@", getOrientationUpdates?@"YES":@"NO"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; ``` with `-(void)orientationChanged: (NSNotification*) notification { NSLog(@"orientationChanged"); }` and ` [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] removeObserver:self];` respectively. when I do `beginGeneratingDeviceOrientationNotifications` etc. in the AppDelegate's `- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ` method, orientationChanged: is called once on startup but never again however I rotate the device, when I do it in one of the UIViewController subclasses it is never called! So far, all I want to achieve is getting orientation notifications to rotate an UIImageView and UIImage (without any layout changes in the different orientations). ``` UIDeviceOrientation o = [UIDevice currentDevice].orientation; ``` always returns `UIDeviceOrientationPortrait` It might be that I missed something in the docs or on stackoverflow, but I obviously cannot figure out what I need to do/add to get it working in my setup. also I am quite new to stackoverflow, so I hope my post is not violating any platform conventions. Any help/hints are greatly appreciated. Thank you so much! EDIT: getOrientationUpdates is always YES, which appears strange to me as the notification callback selector is never called when I rotate it! EDIT: in my AppDelegate's didFinishLaunchingWithOptions I am doing: ``` self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.regScreenController = [[RegistrationScreenController alloc] initWithNibName:@"RegistrationScreenController" bundle:nil]; navCtrl = [[UINavigationController alloc]initWithRootViewController:self.regScreenController]; [navCtrl setNavigationBarHidden:YES]; self.window.rootViewController = self.regScreenController; [self.window addSubview:navCtrl.view]; [self.window makeKeyAndVisible]; return YES; ```

Original source