2013-01-09 42 views
0

真的一直困住这个星期。我使用的是ECSlidingViewController,我想要一个视图,可以旋转为横向和纵向,因为它是风景照片,需要利用可用空间,而我不希望应用的其余部分旋转,只是留在风景。[self.view addSubview:_topViewController.view];自转问题

我敢肯定的自转方法没有得到所谓的,因为它使用这种技术来切换到视图...

- (void)setTopViewController:(UIViewController *)theTopViewController 
{ 
    CGRect topViewFrame = _topViewController ? _topViewController.view.frame : self.view.bounds; 

    [self removeTopViewSnapshot]; 
    [_topViewController.view removeFromSuperview]; 
    [_topViewController willMoveToParentViewController:_topViewController]; 
    [_topViewController removeFromParentViewController]; 

    _topViewController = theTopViewController; 

    [self addChildViewController:self.topViewController]; 
    [self.topViewController didMoveToParentViewController:self]; 

    [_topViewController.view setAutoresizingMask:self.autoResizeToFillScreen]; 
    [_topViewController.view setFrame:topViewFrame]; 
    _topViewController.view.layer.shadowOffset = CGSizeZero; 
    _topViewController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.layer.bounds].CGPath; 

    [self.view addSubview:_topViewController.view]; 
} 

而且我initialviewcontroller ...

self.topViewController = [storyboard instantiateViewControllerWithIdentifier:@"Home"]; 

所以这只是堆叠在顶部,而不是切换到这个视图。因此,它总是听初始视图控制器的旋转方法...

帮助是非常赞赏,因为我说我已经坚持了好几天......

+0

你的问题不清楚。你可以添加一些更多的问题的解释,也许增加一些你的代码。 – foundry

+0

那么,我已经构建了一个应用程序,我想除了一个视图以外的所有视图都是纵向的,所以我添加了自动旋转的iOS6方法,但它们没有被调用为[self.view addSubview:_topViewController.view];添加到堆栈顶部我猜,看看链接,你很快就会明白它是如何工作的。我只需要能够使用自动旋转方法,如 ' - (NSUInteger)supportedInterfaceOrientations, - (BOOL)shouldAutorotate, - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation' – rjg

+0

请不要在问题中喊出来。这很烦人,给别人留下不好的印象。 –

回答

1

做同样的挣扎了几个小时后,我终于能够使这项工作..

首先,你需要创建的ECSLidingViewController子视图和把这个代码:

-(NSUInteger)supportedInterfaceOrientations{ 
    return [self.topViewController supportedInterfaceOrientations]; 
} 

-(BOOL)shouldAutorotate{ 
    return self.topViewController.shouldAutorotate; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
    return [self.topViewController preferredInterfaceOrientationForPresentation]; 
} 

您还需要创建一个UINavigationController类别和覆盖此代码

@implementation UINavigationController (Rotation_IOS6) 

-(BOOL)shouldAutorotate{ 
    return [[self.viewControllers lastObject] shouldAutorotate]; 
} 

-(NSUInteger)supportedInterfaceOrientations{ 
    return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
} 

@end 

希望这项工作为你..

+0

这就是我最终得到它的原因!刚刚看到这个,哥们儿欢呼! – rjg

0

这是一个常见的问题:如果你创建一个视图控制器,通过addSubview将其视图添加到另一个视图控制器的视图中,那么第一个控制器将不会获得任何相关的自动调用。

为了应对这种使用情况,Apple在iOS5中添加了所谓的UIViewController遏制,您可以在其中添加子控制器到另一个控制器;那么所有相关的方法(viewWillAppear/Disappear; autorotation方法等都将被自动路由到子控制器)。

以下是你可能需要调用的基本方法:

addChildViewController: 
removeFromParentViewController 
willMoveToParentViewController: 
didMoveToParentViewController: 

看一看Implementing a Container View Controller详细信息。

Here你可以找到一个教程,将为您提供如何使用遏制的分步说明。

请记住,这只适用于iOS> 5,所以如果您需要支持iOS4,那么您运气不好。无论如何,您可能会尝试通过将相关消息中继到您的子控制器来构建解决方法。例如,在我的应用程序,这是willAnimateRotationToInterfaceOrientationdidRotateFromInterfaceOrientation样子:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
            duration:(NSTimeInterval)duration { 

[self.mainButtonBarController willAnimateRotationToInterfaceOrientation:interfaceOrientation 
                   duration:duration]; 
[self.boardController willAnimateRotationToInterfaceOrientation:interfaceOrientation 
                   duration:duration]; 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 

[self.mainButtonBarController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 
[self.boardController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 
} 

即,他们只是提出了同样的消息,孩子控制器。您可以为需要其他的方法(shouldAutorotate...等)

+0

感谢您的回答,总新手,但我会将此添加到ECSliding类,如下所示:'[_topViewController willMoveToParentViewController:self]; [self addChildViewController:_topViewController]; [self.view addSubview:_topViewController.view]; [_topViewController didMoveToParentViewController:self];' – rjg

+0

你可以看看ECSliding,并告诉我在哪里实现这个代码等,我很抱歉是一个有害生物,但完全失去了......非常感谢,非常感谢! – rjg

+0

我不知道'ECSliding',但我不认为你需要修改'ECSliding';简单地说,当你调用'addSubview'时,也调用包含相关的方法......如果你需要更多的帮助,发布你的问题,你创建ECSlidingController的方式以及你如何添加子控制器。 – sergio