【发布时间】:2014-07-10 09:38:37
【问题描述】:
我有五个视图控制器,并且在项目目标设备方向中我启用了纵向、横向左侧和横向右侧。现在我想要 5 个视图控制器中的 4 个视图控制器保持纵向模式(不旋转到横向左侧和横向右侧),并且只有一个视图控制器在所有模式下旋转(纵向、横向左侧、横向右侧)。那么如何做到这一点请告诉。
【问题讨论】:
-
你之前尝试过什么?
标签: ios iphone objective-c rotation
我有五个视图控制器,并且在项目目标设备方向中我启用了纵向、横向左侧和横向右侧。现在我想要 5 个视图控制器中的 4 个视图控制器保持纵向模式(不旋转到横向左侧和横向右侧),并且只有一个视图控制器在所有模式下旋转(纵向、横向左侧、横向右侧)。那么如何做到这一点请告诉。
【问题讨论】:
标签: ios iphone objective-c rotation
为每个 ViewController 实现 -(NSUInteger)supportedInterfaceOrientations 并指定每个控制器应支持的界面方向。
编辑
假设您对每个 ViewController 都有单独的实现,请在每个实现中实现 -(NSUInteger)supportedInterfaceOrientations 和 -(BOOL)shouldAutorotate。
例如
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
将确保您的视图控制器支持所有横向模式。结合这个
-(BOOL)shouldAutorotate{
return YES;
}
你的显示器会在旋转时“翻转”。
使用枚举 UIInterfaceOrientationMask 调整支持的方向,并尝试不同的组合以及对 -(BOOL)shouldAutorotate 的返回值是/否,直到获得所需的行为。
【讨论】:
首先,在 AppDelegate 中,这样写。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
For UIViewControllers, which require LANDSCAPE too, change masking to All.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
//OR return UIInterfaceOrientationMaskAll;
}
Now, if you want to do some changes when Orientation changes, then use this function.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
请注意:-
很大程度上取决于您的 UIViewController 嵌入到哪个控制器中。
例如,如果它在 UINavigationController 中,那么您可能需要继承该 UINavigationController 以覆盖这样的方向方法。
子类 UINavigationController(层次结构的顶部视图控制器将控制方向。)确实将其设置为 self.window.rootViewController。
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
从 iOS 6 开始,UINavigationController 不会向其 UIVIewControllers 请求方向支持。因此我们需要对它进行子类化。
【讨论】: