故事板方向支持xCode 4.2? [英] Storyboards orientation support for xCode 4.2?

查看:153
本文介绍了故事板方向支持xCode 4.2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我升级到xCode 4.2,它是新的Storyboards功能。然而,找不到支持肖像和风景的方法。

I upgraded to xCode 4.2 and it's new Storyboards feature. However, could not find a way to support both portrait and landscape.

当然,我是以编程方式完成的,有2个视图,一个用于纵向,一个用于横向,如在过去,和:

Of course, I did it programmatically, with 2 views, one for portrait and one for landscape, like in old days, and:

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

但我一直在想办法以某种方式自动执行此操作。我的意思是,现在是xCode 4.2,我期待更多。谢谢大家。

But I was looking for a way to do this automatically somehow. I mean, it's xCode 4.2 now, I expected more from it. Thanks all.

================================ ==

临时解决方案:

==================================
TEMPORARY SOLUTION:

我将在此提供一个临时解决方案。我说这是暂时的,因为我还在等待Apple工作人员做一些非常聪明的事情。

I will present here a temporary solution. I say it's temporary, because I am still waiting for Apple guys to do something really intelligent about this.

我创建了另一个.storyboard文件,名为MainStoryboard_iPhone_Landscape,并实现了那里的景观视图控制器。实际上,它与普通(纵向).storyboard完全一样,但所有屏幕都处于横向模式。

I created another .storyboard file, called "MainStoryboard_iPhone_Landscape", and implemented the landscape view controllers there. Actually, it's exactly like normal(portrait) .storyboard, but all screens are in landscape mode.

因此,我将从横向故事板中提取ViewController,并在轮换发生时,只需使用新的viewController视图更改self.view。

So I will extract the ViewController from landscape storyboard, and when rotation occurs, just change self.view with the new viewController's view.

1.方向更改时生成通知:

1.Generate Notifications when orientation changes:

[[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.Implement updateLandscapeView

3.Implement 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示例来提出这个解决方案。基本上它正在为景观视图提供模态视图。

This is an old question but I read this earlier in the day and then had to spend a fair amount of time work out a better solution. I came up with this solution from hacking up the Apple Alternate View example. Basically it is serving up a modal view for the landscape 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);
}

这篇关于故事板方向支持xCode 4.2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆