How to Manage iPhone Rotations in Tabbed Application?

In classic view-based iPhone application it is enough to implement view controller's


method for managing device orientation. Unfortunately it doesn't work with tab-based application. I can't guarantee that my solution is the best, but I found a simple way around.

Let's take a look at this solution step by step.

Subclass the UITabBarController and use its instance in application instead of a standard one.

@interface MyTabBarController : UITabBarController { } @end //---------------------------------------------------------------------- @implementation MyTabBarController -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { /* Orientation managing logic * } @end

In case AutorotateToInterfaceOrientation method returns YES I used default notification center to post appropriate notification. Also it is very useful to pass new interface orientation via userInfo dictionary.

NSNumber *orientationNumber = [NSNumber numberWithInt:interfaceOrientation]; SDictionary *userInfo = [NSDictionary dictionaryWithObject:orientationNumber forKey:@"UserInfo_DeviceOrientation"]; [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:@"NTF_DEVICE_SHOUD_CHANGE_ORIENTATION" object:self userInfo:userInfo]];

Now if any view controller is interested in changing orientation it can simply register itself as an observer for NTF_DEVICE_SHOUD_CHANGE_ORIENTATION notification.

I consent Lemberg Solution Ltd collecting and using provided personal information as set out in the Privacy Policy.

Lemberg is a technology consulting, software & hardware engineering company.

Startups and established businesses rely on our industry expertise to build new products and deliver digital transformation.

Join our Newsletter?
I agree to receive emails from Lemberg Solutions Ltd.