Created
May 25, 2011 05:00
-
-
Save chrishulbert/990371 to your computer and use it in GitHub Desktop.
Finding the currently visible view in a UITabBarController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
// Firstly, we want to figure out who is the currently visible view controller. | |
// There are 4 possibilities here: | |
// 1- They could be a non-nav controller, with a visible tab (ie not under the 'more' tab) | |
// Simple: self.selectedViewController | |
// 2- They could be a nav controller, with a visible tab (ie not under the 'more' tab) | |
// Get it's visible view: self.selectedViewController.visibleViewController | |
// 3- They could be a non-nav controller, under the 'more' tab | |
// Simple: self.selectedViewController | |
// 4- They could be a nav controller, under the 'more' tab | |
// This is where it gets tricky (or buggy, really? You decide.) | |
// What UIKit does is: its sets the self.selectedViewController to your nav controller, but steals the root view and | |
// pushes it onto the self.moreNavigationController's stack | |
// So if your self.selectedViewController.viewControllers is empty, use self.moreNavigationController.visibleViewController | |
UIViewController *visible = self.selectedViewController; // For non-nav controllers | |
if ([visible respondsToSelector:@selector(visibleViewController)]) // For nav controllers | |
visible = [((UINavigationController*)visible) visibleViewController]; | |
if (!visible) // Exception for nav controllers under the more tab | |
visible = self.moreNavigationController.visibleViewController; | |
return [visible shouldAutorotateToInterfaceOrientation:interfaceOrientation]; | |
} |
No worries :)
Yeah i've got no idea about whether things have changed in ios5 to be
honest. And really, if you've got a 'more' tab in your tab view, you
probably should pare your app down somewhat anyway. Anyway good luck
finding a solution that works!
…On Thu, May 10, 2012 at 11:05 PM, Joe D'Andrea < ***@***.*** > wrote:
Thanks! I really appreciate your patience and willingness to indulge me
here in the comments.
As for that blog post, yep, that's how I arrived here. It's <a href="
http://splinter.com.au/finding-the-currently-visible-view-in-a-uitab">this
one</a>, right?
The thing that's got me confused now is that visibleController shouldn't
work (at least certainly not on iOS 5, with those two private VCs that
Apple exposes in there), but that's what you use in your code ... unless
Apple changed something subtle as of iOS 5.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/990371
Thanks, and you're 100% correct - I want to lose that More tab so badly, you have no idea.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! I really appreciate your patience and willingness to indulge me here in the comments.
As for that blog post, yep, that's how I arrived here. It's this one, right?
The thing that's got me confused now is that visibleController shouldn't work (at least certainly not on iOS 5, with those two private VCs that Apple exposes in there), but that's what you use in your code ... unless Apple changed something subtle as of iOS 5.