我正在为iOS5和iOS6构建一个应用程序.我在UINavigationController中有这个UIViewController,我希望它保持纵向模式.
该代码适用于iOS5,但不适用于iOS6.
// iOS5 rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
return YES;
else
return NO;
}
// iOS6 rotation
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
这有什么问题??? 在SO上我发现了许多类似的问题,但一般来说答案对我不起作用.
编辑: 也许我不准确,但我需要一个单一的视图控制器(我的应用程序的主页)保持纵向模式而不是所有的应用程序.
首先,很大程度上取决于你的UIViewController嵌入哪个控制器.
例如,如果它在UINavigationController中,那么您可能需要将UINavigationController子类化为覆盖这样的方向方法.
子类化UINavigationController(层次结构的顶层视图控制器将控制方向.)需要将其设置为self.window.rootViewController.
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
Run Code Online (Sandbox Code Playgroud)
从iOS 6开始,UINavigationController不会要求其UIVIewControllers提供方向支持.因此我们需要将其子类化.
此外
然后,对于只需要PORTRAIT模式的UIViewControllers,编写这些函数
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait);
}
Run Code Online (Sandbox Code Playgroud)
对于也需要LANDSCAPE的UIViewControllers,将掩码更改为All.
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAllButUpsideDown);
//OR return (UIInterfaceOrientationMaskAll);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果要在Orientation更改时进行一些更改,请使用此功能.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
Run Code Online (Sandbox Code Playgroud)
很重要
在AppDelegate中,写下这个.这是非常重要的.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}
Run Code Online (Sandbox Code Playgroud)
如果您只想为所有视图控制器提供纵向模式,请应用portait掩码.即UIInterfaceOrientationMaskPortrait
否则,如果您希望某些UIViewControllers保留在Portrait中,而其他人支持所有方向,则应用ALL Mask.即UIInterfaceOrientationMaskAll
| 归档时间: |
|
| 查看次数: |
5696 次 |
| 最近记录: |