Reputation: 1237
I have created a subclass of UITabBarController to deal with the autoRotation of views when I switch as I need some views to be portrait and others landscape.
If I rotate the device this all works as expected, so the landscape ones will rotate to landscape and stay that way. The issue I am having is that the rotation isn't automatically performed when the view shows. So the views that are meant to be landscape are shown as portrait until the device is rotated to landscape, at which point they will lock it to landscape mode.
I was wondering if anyone can tell me how to force the rotation upon view change, so when I switch to a landscape view it will rotate to that orientation straight away?
Sorry if this has been asked a million times before but I can't seem to find the threads.
EDIT, I HAVE FOUND THE FOLLOWING CODE:
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
This does perform the rotation, but I'm not sure where to call it as the tab bar controller doesn't call things such as init / viewDidLoad, obviously putting it in viewDidAppear etc will cause an infinite loop.
Any help on this please?
Upvotes: 0
Views: 1180
Reputation: 2699
@Elliott, I gave you an upvote for your answer because it works better than any other solution I've tried, but here's a simplification. Instead of using a notification, just put this in didSelectViewController:
if ( [viewController respondsToSelector:@selector(updateViewRotation)] ) {
[viewController performSelector:@selector(updateViewRotation)];
}
That only works if the viewController needing this special attention is the first one on the tab.
Also, updating for iOS 6 deprecations:
- (void)updateViewRotation
{
UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
}
Upvotes: 2
Reputation: 1237
I came up with a solution to my problem and will post it here just incase other people want it. I warn you now it is a work around, but I couldn't find an answer so it was the best I could do.
First add the UITabBarControllerDelegate to your protocols in which the TabBarController is being created, I am using the AppDelegate so do the following:
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
This allows access to the following TabBar protocol:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
This basically gets called whenever one the of TabBarItems is pressed (buttons along the bottom). Within this function I am extracting the name of the viewControllerNibFile as this is what they are named (you can probably use the view.tag if you don't want to worry about naming conventions), and posting an event to the notification center as follows:
NSString *notificationName;
if ([viewController.nibName isEqualToString:@"IntroViewController"])
notificationName = [NSString stringWithString:@"IntroViewChange"];
NSLog(@"%@", notificationName);
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil];
Then I setup a listener in the class which calls the following event:
- (void)updateViewRotation
{
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
}
Like mentioned before this is a massive work around, it's fairly weighty and ugly to look at but it does what I need. If anyone wants to offer some more efficient code I would happily take it.
Upvotes: 3