2014-12-31 37 views

回答

0

幾周前我面臨同樣的問題。我創建了新的縱向UIViewController。後來我意識到改變方向是不夠的,然後我使用下面的代碼來改變方向。使用下面的代碼來改變方向。

-(void)viewWillAppear:(BOOL)animated 
    { 
     [self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:.2]; 
     [self awakeFromNib]; 

    } 
    -(void)viewWillDisappear:(BOOL)animated { 

     [[NSNotificationCenter defaultCenter] removeObserver:self                      name:UIDeviceOrientationDidChangeNotification 
                object:nil]; 
    } 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
    { 
     if(is_iPad) 
     { 
     return YES; 
     }else 
     { 
     return (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)); 
     } 
    } 


- (void)awakeFromNib 

{ 
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 
    delegate->isShowingLandscapeView=NO; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 

              selector:@selector(orientationChanged:) 

               name:UIDeviceOrientationDidChangeNotification 

              object:nil]; 

} 

- (void)orientationChanged:(NSNotification *)notification 
{ 
    if (is_iPad) { 
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && 
     !delegate->isShowingLandscapeView) 
    { 
     yourPotraitViewController *ypvc =[[yourPotraitViewController alloc] initWithNibName:@"yourPotraitViewController" bundle:nil]; 


     [self.navigationController pushViewController:objHomeLandscape animated:YES]; 
     delegate->isShowingLandscapeView=YES; 
     // isShowingLandscapeView = YES; 
    } 
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) && 
      delegate->isShowingLandscapeView) 
    { 
     [self.navigationController popViewControllerAnimated:YES]; 
     delegate->isShowingLandscapeView=NO; 
    } 
    } 
} 
相關問題