我升级到 xCode 4.2,它是新的 Storyboards 功能。但是,找不到同时支持纵向和横向的方法。
当然,我是以编程方式完成的,有 2 个视图,一个用于纵向,一个用于横向,就像过去一样,并且:
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view = self.landscapeView;
}
else
{
self.view = self.portraitView;
}
但我一直在寻找一种以某种方式自动执行此操作的方法。我的意思是,现在是 xCode 4.2,我期待更多。谢谢大家。
====================================
临时解决方案:
我将在这里提出一个临时解决方案。我说这是暂时的,因为我仍在等待苹果公司对此做一些真正聪明的事情。
我创建了另一个 .storyboard 文件,名为“MainStoryboard_iPhone_Landscape”,并在那里实现了横向视图控制器。实际上,它与 normal(portrait) .storyboard 完全一样,但所有屏幕都处于横向模式。
所以我将从横向故事板中提取 ViewController,当发生旋转时,只需将 self.view 更改为新的 viewController 的视图。
1.当方向改变时生成通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
2.寻找通知:
[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}];
3.实现updateLandscapeView
- (void)updateLandscapeView {
//> isShowingLandscapeView is declared in AppDelegate, so you won't need to declare it in each ViewController
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !appDelegate().isShowingLandscapeView)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_Landscape" bundle:[NSBundle mainBundle]];
MDBLogin *loginVC_landscape = [storyboard instantiateViewControllerWithIdentifier:@"MDBLogin"];
appDelegate().isShowingLandscapeView = YES;
[UIView transitionWithView:loginVC_landscape.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
//> Setup self.view to be the landscape view
self.view = loginVC_landscape.view;
} completion:NULL];
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) && appDelegate().isShowingLandscapeView)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
MDBLogin *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"MDBLogin"];
appDelegate().isShowingLandscapeView = NO;
[UIView transitionWithView:loginVC.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
//> Setup self.view to be now the previous portrait view
self.view = loginVC.view;
} completion:NULL];
}}
祝大家好运。
PS:我会接受 Ad Taylor 的回答,因为经过长时间的等待和寻找解决方案,我完成了从他的回答中获得灵感的一些东西。谢谢泰勒。