Reputation: 630
So, I want to lock the orientation of my home page to portrait, and the Home page ONLY.
I am using a tab bar controller, so the initial view is the tab controller, but the view controller that appears first is the first tab, e.g. the Home page.
I would like to make it so that when the user goes to rotate the device, it WILL NOT rotate to landscape on this page. However all other pages can rotate.
I have searched around, and nothing seems to be specific to iOS 7, and the one that is specific to iOS 7 doesn't work…
Please help, thank you!
The image below describes what I DON"T want to happen, for this page.
Upvotes: 10
Views: 11991
Reputation: 2395
I think I found a nice solution. Well, in my case I'm using a UISplitViewController as rootController in the storyboard but the idea is the same.
Implement shouldAutorotate() in the View you want to Lock the Rotation
class MyUISplitViewController: UISplitViewController {
override func shouldAutorotate() -> Bool {
if ((self.viewControllers.last) != nil && (self.viewControllers.last!.topViewController) != nil){
if (self.viewControllers.last!.topViewController!.respondsToSelector("shouldAutorotate"))
{
return self.viewControllers.last!.topViewController!.shouldAutorotate()
}
}
return true
}
}
In your sub UIViewController
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().userInterfaceIdiom == .Phone)
{
return false
}else{
return true
}
}
If you want to check the supported orientations, you can simply do the same with supportedsupportedInterfaceOrientations()
EDIT:
Don't forget to set your "MyUISplitViewController" class in your Storyboard root viewController
Upvotes: 0
Reputation: 173562
The problem is, as you've rightly pointed out, that your home tab is not the topmost view controller.
From my limited knowledge on the subject I can only think of the following:
shouldAutorotate
and supportedInterfaceOrientations
;Upvotes: 0
Reputation: 772
Use this code
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return UIInterfaceOrientationMaskPortrait;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
@end
Upvotes: 6
Reputation: 2450
Implement the following in your implementation
- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
This should give you the results you are looking for!
Upvotes: 7