我知道以前有人问过类似的问题,但这是一个更具体的用例(从静态库控制方向,不能在根视图控制器中写入)。
我有一个静态库,它将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中运行的解决方案
发布于 2015-09-30 22:25:55
在整个应用程序中,您不能仅锁定一个视图控制器的方向。因此,在启动视图和更改方向时,必须使用父视图框设置变换角度。
您可以为这些视图控制器类添加代码
- (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)];
}
}}
UIInterfaceOrientation orientation = [UIApplication sharedApplication statusBarOrientation];if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { [self view setTransform:CGAffineTransformMakeRotation(M_PI_2)];:CGRectMake( 0,0,self.view.frame.size.height,self.view.frame.size.width)];CGFloat弧度=atan2f(self.view.trans.b,self.view.transform.a);if ( radians !=M_PI_2) { [self view setBound
}
}否则{
}
} }
https://stackoverflow.com/questions/16947988
复制相似问题