我以前从未这样做过——像这样更改 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;
}
}