この問題は私を一週間殺していた。私はまったく同じ状況にあり、1つのビューに対しても回転できるようにしたかっただけです。タブ付きアプリでそれを回避する方法は、オリエンテーション通知に登録することです。TabController(およびその他のsuperView)と回転するVCのshouldAutoRotateに対してNOを返します。次に、viewWillAppearで通知を登録します。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
そして、呼び出される関数は次のようになります。
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeLeft:
if(viewHasAppeared==true){
[self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
break;
case UIDeviceOrientationLandscapeRight:
if(viewHasAppeared==true){
[self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
break;
default:
break;
};
}
-ビューが実際に表示されるまでセグエできないため、ViewDidAppear内にブール値を配置します。
ランドスケープモードで変更するビューがタブ付きのViewControllerでない場合、それを処理する最良の方法は、そのビューでAutoRotationを許可し、willAnimateで次のようにVCを閉じることです。
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
[self dismissViewControllerAnimated:YES completion:nil];
}
}
タブ付きのビューの場合は、そのビューでも向きの変更を登録するだけです。ただし、ビューを離れるときは、必ずorientationChange通知を削除してください。そうしないと、他のviewControllerからもランドスケープに移行することになります。
また、別のタブに移動する場合は、tabBarControllerデリゲートを実装し、次のようにオブザーバーを削除する必要があります。
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if(![NSStringFromClass([viewController class])isEqualToString:@"FirstViewController"])
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}
}