2014-09-19 93 views
0

在我的設計中,有一個按鈕用於更改xib文件。此前谷歌一千次,我找到了解決辦法如下顯示:更改xib文件後錯誤的視圖方向

- (IBAction)changeButtonDidPress:(id)sender 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     if (self.xibChanged) { 
      NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"MyViewController" owner:self options:nil]; 
      UIView *aView = [nibObjs objectAtIndex:0]; 
      self.view = aView; 
      self.xibChanged = NO; 
     } else { 
      NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"CustomViewController" owner:self options:nil]; 
      UIView *aView = [nibObjs objectAtIndex:0]; 
      self.view = aView; 
      self.xibChanged = YES; 
     } 
    }); 
} 

但是,有一個問題:後改變廈門國際銀行文件是錯誤的(總是肖像,不管我怎麼設置廈門國際銀行文件的視圖的方向)!
ps:我也試過:
1)Change view in one XIB,運行良好,但這意味着我需要創建一個新的控制器,我從來不需要!
2)Single UIViewcontroller, how to switch among multiple xibs?,根本沒有工作,並且在新視圖中丟失了一些按鈕!
所以,我發佈這個問題,有人遇到類似的問題?任何好主意?謝謝!

回答

0

嗯,經過一千次的重試,我剛剛發現一個解決方法:rotate your new view that has wrong orientation
第一次開始時,請確定xibs使用自動調整,否則你應該多做點努力!
則此方法添加到您的ViewController:

- (void)rotateUIView:(UIView *)aView toInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Get data of rotating view 
    CGRect bounds = [[UIScreen mainScreen] bounds]; 
    CGFloat angle = 0.0f, width = 0.0f, height = 0.0f; 
    switch (interfaceOrientation) { 
     case UIInterfaceOrientationPortraitUpsideDown: 
      angle = M_PI; 
      width = bounds.size.width; 
      height = bounds.size.height; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
      angle = -M_PI_2; 
      height = bounds.size.width; 
      width = bounds.size.height; 
      break; 
     case UIInterfaceOrientationLandscapeRight: 
      angle = M_PI_2; 
      height = bounds.size.width; 
      width = bounds.size.height; 
      break; 
     case UIInterfaceOrientationPortrait: 
     default: 
      return; 
    } 

    // Rotate the view 
    [aView setBounds:CGRectMake(0, 0, width, height)]; 
    [aView setTransform:CGAffineTransformConcat(aView.transform, CGAffineTransformMakeRotation(angle))]; 
} 

調用此方法改變廈門國際銀行文件後,具體如下:

[self rotateUIView:self.view toInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; 

然後視圖的取向是所有權利!希望這也能解決你的問題!