我以前从未这样做过——像这样更改 ViewController 的 UIView,所以虽然它似乎工作得很好,但我不确定这是否是个好主意——是否会导致任何重大错误。它使用 Storybards 但 viewLandscape
从 nib
加载
所以我开始在 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/