xib文件更改后视图的方向错误

时间:2014-09-19 05:58:40

标签: ios objective-c xcode 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;
        }
   });
}

但是,有一个问题:更改xib文件后视图的方向是错误的(总是肖像,无论我如何设置xib文件)!
ps:我也尝试过:
1)Change view in one XIB,运作良好,但这意味着我需要创建一个新的控制器,这是我从未想过的!
2)Single UIViewcontroller, how to switch among multiple xibs?,根本没用,有些按钮在新视图中丢失了!
所以,我发布这个问题,有人遇到过类似的问题吗?有什么好主意吗?谢谢!

1 个答案:

答案 0 :(得分: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))];
}

更改xib文件后调用此方法,如下所示:

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

那么视图的方向是可以的!希望这也能解决你的问题!