2014-09-19 96 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]]; 

然后视图的取向是所有权利!希望这也能解决你的问题!