仅为一个视图控制器从静态库中维护iOS竖屏朝向



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

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

我想将朝向锁定为竖屏,只针对视图控制器。在iOS 6中,当我在库的视图控制器中使用以下代码时,它根本不影响autorotate的行为:

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

当我在根视图控制器中放入相同的代码时,它可以完美地工作,应用程序不再自动旋转。然而,这对我们来说不是一个选项,因为在生产环境中,我们将无法访问客户端的根视图控制器。是否有一种方法可以从非根视图控制器锁定视图方向,或者仅锁定单个视图控制器的方向?还有其他我没想到的方法吗?

希望能够在iOS上运行<= 6

你不能在整个应用程序中只锁定一个视图控制器的方向。所以你必须在视图启动和方向改变时用父视图框设置transform angle

你为那些视图控制器类添加代码

- (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: cgaffinetransformmakeroation (M_PI_2)];

    }
    
    其他

    } {

    [[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(弧度!=0){[[self view] setTransform: cgaffinetransformmakeroation (0)];

    }
    

    }}

最新更新