這個問題在一個星期內讓我失望。我有完全相同的情況,只希望能夠旋轉一個視圖。您可以在選項卡式應用程序中避開它的方式是註冊定位通知。在您的TabController(以及其他superViews)和您要旋轉的VC中返回NO for shouldAutoRotate。然後,在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;
};
}
-put ViewDidAppear內一個布爾值,因爲你不能Segue公司,直到上實際的看法。
現在,如果您在橫向模式下更改的視圖不是標籤式視圖控制器,那麼處理該問題的最佳方式是隻允許在該視圖中使用AutoRotation,然後在willAnimate中將像這樣解除VC:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
[self dismissViewControllerAnimated:YES completion:nil];
}
}
如果是標籤式查看,那麼只需在該視圖中註冊orientationchanges。但是,確保在離開視圖時刪除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]];
}
}
我要投票這件事。我試圖弄清楚這一點,並在這裏得到沒有牽引在這個相同的問題 –