Monday, January 5, 2015

Dynamically changing rotation in iOS

I had a requirement to dynamically enable and disable the rotation of an iOS. We had a navigation controller throughout the app. First you need to enable landscape and potrait mode using Xcode. You can specify this in the deployment info section in the XCode
When we dynamically change the rotation, we need to change the rotation not only in the viewcontroller, but also in the navigation controller. Next you need to create a category of UINavigationController which supports for dynamic orientation changes. Here you need to overide two methods

-(bool)shouldAutoRotate and -(NSUInteger)supportedInterfaceOrientation
This is how they were overridden.



We need a boolean instance variable to determine whether we should rotate or not. Here. I have used iOS run time programming to assign value for this boolean value. Because we can't synthasis instance variables in a category. Also I am using a NSNumber variable instead of a boolean primitive.



Here KNewProertyKey is declared as follows


When you need to enable or disable rotation you just need to set the status with setRotationStatus method.
After that we can dynamically enable and disable the rotation. For any query, feel free to contact me via my linkedin profile.

No comments:

Post a Comment