1

我以前从未这样做过——像这样更改 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(Pointer from the nib to the View Controller for the storyboard view)

-(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;
    }
}
4

1 回答 1

3

执行此操作的常用方法是为水平设置不同的视图控制器,并通过交叉淡入淡出从垂直 VC 呈现该视图控制器。在View Controller Programming Guide中的“Creating an Alternate Landscape Interface”下有更多信息。

于 2013-09-08T19:57:29.343 回答