gpt4 book ai didi

iphone - xCode 4.2 的 Storyboard 方向支持吗?

转载 作者:行者123 更新时间:2023-12-03 18:26:00 27 4
gpt4 key购买 nike

我升级到 xCode 4.2,它是新的 Storyboard功能。但是,找不到同时支持纵向和横向的方法。

当然,我是通过编程方式完成的,有 2 个 View ,一个用于纵向,一个用于横向,就像以前一样,并且:

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
{
self.view = self.landscapeView;
}
else
{
self.view = self.portraitView;
}

但我一直在寻找一种以某种方式自动执行此操作的方法。我的意思是,现在是 xCode 4.2,我对它有更多期望。谢谢大家。

====================================
临时解决方案:

我在这里提出一个临时解决方案。我说这是暂时的,因为我仍在等待苹果公司的人员对此做出一些真正明智的事情。

我创建了另一个 .storyboard 文件,名为“MainStoryboard_iPhone_Landscape”,并在那里实现了横向 View Controller 。实际上,它与普通(纵向).storyboard 完全相同,但所有屏幕都是横向模式。

因此,我将从横向 Storyboard中提取 ViewController,并且当发生旋转时,只需使用新 viewController 的 View 更改 self.view 即可。

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

祝大家好运。

P.S:我会接受 Ad Taylor 的答案,因为经过长时间的等待和寻找解决方案,我完成了从他的答案中得到启发的一些实现。谢谢泰勒。

最佳答案

这是一个老问题,但我在当天早些时候读过这个问题,然后不得不花费相当多的时间来找出更好的解决方案。我通过破解Apple Alternate View example想出了这个解决方案。基本上它为景观 View 提供了一个模态视图。

#pragma mark Rotation view control

- (void)orientationChanged:(NSNotification *)notification
{
// 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];
}

- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !self.isShowingLandscapeView)
{
[self performSegueWithIdentifier: @"toLandscape" sender: self];
self.isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && self.isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
self.isShowingLandscapeView = NO;
}
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

关于iphone - xCode 4.2 的 Storyboard 方向支持吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7803524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com