【问题标题】:Replacing UIViews when screen rotates in the same View Controller当屏幕在同一个视图控制器中旋转时替换 UIViews
【发布时间】:2013-09-08 19:35:11
【问题描述】:

我以前从未这样做过 - 像这样更改 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 中获取指向该标签的指针(从笔尖指向情节提要视图的视图控制器的指针)

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

【问题讨论】:

    标签: ios uiview uiviewcontroller nib autorotate


    【解决方案1】:

    执行此操作的常用方法是使用不同的水平视图控制器并通过交叉淡入淡出从垂直 VC 呈现该视图控制器。在 View Controller Programming Guide 的“创建备用景观界面”下有更多相关信息。

    【讨论】:

    • 谢谢!我的解决方案有问题,因为我在标签栏控制器中有两个视图,当我转到另一个视图时,旋转设备并返回此视图 - 将加载错误的视图。而且 Apple 几乎不可能在 iOS 6 中以编程方式改变方向。在我放弃之前,我花了好几个小时想办法做到这一点。
    猜你喜欢
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    相关资源
    最近更新 更多