仅针对一个视图控制器从静态库中维护iOS的纵向方向

6

我知道之前有类似的问题,但这是一个更具体的用例(从静态库控制方向,无法在根视图控制器中编写)。

我有一个静态库,将UI元素作为叠加层添加到传递的视图控制器(客户端的根视图控制器)作为子视图。问题是,我们的UI元素仅支持纵向方向,而客户端的应用程序可能支持横向和纵向。只要我们的UI元素不随客户端视图自动旋转,这是可以的。

我想仅针对我们的视图控制器锁定方向为纵向。在iOS 6中,当我在我的库的视图控制器中使用以下代码时,它根本不影响自动旋转的行为:

-(BOOL)shouldAutorotate{
return NO;
}

-(NSInteger)supportedInterfaceOrientations{
    NSInteger orientationMask = 0;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
        orientationMask |= UIInterfaceOrientationMaskPortrait;
    return orientationMask;
}

当我将相同的代码放在根视图控制器中时,它可以完美地工作,应用程序不再自动旋转。然而,这对我们来说并不是一个选项,因为在生产中,我们将无法访问客户端的根视图控制器。有没有一种方法可以从非根视图控制器锁定视图方向,或仅为单个视图控制器锁定方向?还有其他实现所需功能的方法吗?如果可能的话,希望能找到适用于iOS <= 6的解决方案。


当您的元素被实例化时,如何处理设备已处于横向模式的情况? - Walt Sellers
你是否找到了解决方案? - user-123
1个回答

0

在整个应用程序中,您无法仅为一个视图控制器锁定方向。因此,您必须在视图启动时和方向更改时,在父视图框架上设置变换角度。

您可以为这些视图控制器类添加代码。

- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
    [[self view] setBounds:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
    CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a);
    if (radians!=M_PI_2) {
        [[self view] setTransform:CGAffineTransformMakeRotation(M_PI_2)];

    }

}

}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { [[self view] setBounds:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)]; CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a); if (radians!=M_PI_2) { [[self view] setTransform:CGAffineTransformMakeRotation(M_PI_2)]; } }else{ [[self view] setBounds:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a); if (radians!=0) { [[self view] setTransform:CGAffineTransformMakeRotation(0)]; } } }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接