This is a bit tricky, because we have to take into account there may be nav controllers, also there seems to be a quirk (bug?) when a nav controller is in the 'more' tab.
The code I wrote below needed to know this for the purposes of allowing the currently visible view to determine whether or not to allow landscape mode. Hope it helps someone!
- (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]; | |
} |
Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.
(Comp Sci, Hons - UTS)
Software Developer (Freelancer / Contractor) in Australia.
I have worked at places such as Google, Cochlear, Assembly Payments, News Corp, Fox Sports, NineMSN, FetchTV, Coles, Woolworths, Trust Bank, and Westpac, among others. If you're looking for help developing an iOS app, drop me a line!
Get in touch:
chris.hulbert@gmail.com
github.com/chrishulbert
linkedin