ios - 当屏幕在同一个 View Controller 中旋转时替换 UIViews

标签 ios uiview uiviewcontroller nib autorotate

我以前从未这样做过——像这样更改 ViewController 的 UIView,所以虽然它似乎工作得很好,但我不确定这是否是个好主意——是否会导致任何重大错误。它使用 Storybards 但 viewLandscapenib 加载

所以我开始在 ViewDidLoad 中保留一个指向当前 View 的指针,我假设 viewDidLoad 时的第一个 View 是人像,我怎么能绝对确定呢?然后我从 Nib 初始化横向 View 。

// 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;

现在,我根据界面方向设置显示哪个 View ,很简单!但有什么我应该注意的吗?还有一件事,我在横向 View 上有一个标签,如何在 ViewController 中获得指向该标签的指针(从 Nib 指向 Storyboard View 的 View Controller 的指针)
-(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;
    }
}

最佳答案

执行此操作的常用方法是为水平设置不同的 View Controller ,并通过交叉淡入淡出从垂直 VC 呈现该 View Controller 。在 View Controller Programming Guide 中有更多信息。在“创建备用景观界面”下。

关于ios - 当屏幕在同一个 View Controller 中旋转时替换 UIViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18687823/

相关文章:

ios - AMSlideMenu:如何自定义菜单?

ios - 架构 i386 的 8 个重复符号

ios - 在 'tag' 类型的对象上找不到属性 'const _strong id'

ios - 以编程方式调整 UITextField 的大小会自动恢复为旧大小

iOS 对多个 ViewController 使用相同的 xib

ios - UIStoryboardSegue & UIVisualEffectView - 完成 block 退出后模糊效果丢失

ios - Xcode 11 中引入的 "Development Assets"功能是什么以及如何使用它?

objective-c - UIView animateKeyframesWithDuration 与标准动画 block 动画不同

ios - 如何在隐藏或取消隐藏时为 UIView 设置动画

ios - 以编程方式定义和执行 segue