屏幕在同一视图控制器中旋转时替换UIViews

时间:2013-09-08 19:35:11

标签: ios uiview uiviewcontroller nib autorotate

我之前从未这样做过 - 改变这样的ViewController的UIView,所以尽管它似乎工作得很好但我不确定这是不是一个好主意 - 如果它可能导致任何重大错误。它使用了Storybards,但viewLandscape是从nib

加载的

所以我开始在ViewDidLoad中保留一个指向当前视图的指针,我假设viewDidLoad是纵向时的第一个视图,我怎么能完全确定这个?然后我从笔尖初始化景观视图。

// Assumes UIView loaded at start is portrait
self.viewPortrait = [[UIView alloc]initWithFrame:self.view.bounds];
self.viewPortrait = self.view;

UIView *landscapeView = [[[NSBundle mainBundle] loadNibNamed:@"LandscapeView" owner:self options:nil] objectAtIndex:0];
NSLog(@"view %@", landscapeView);
self.viewLandscape = [[UIView alloc]initWithFrame:landscapeView.bounds];
self.viewLandscape = landscapeView;

现在,我根据界面方向设置哪个视图显示,非常简单!但是我应该注意什么?更重要的是,我在横向视图上有一个标签,如何在此ViewController中获取指向该标签的指针(从nib到视图控制器的指针,用于故事板视图)

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"will rotate");
    switch (toInterfaceOrientation)
    {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            self.view = self.viewPortrait;
            break;

        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            self.view = self.viewLandscape;
            break;
    }
}

1 个答案:

答案 0 :(得分:3)

执行此操作的常用方法是使用不同的水平视图控制器,并使用交叉渐变从垂直VC显示该视图控制器。在“创建备用横向界面”下的View Controller Programming Guide中有更多相关信息。