今天在做到横屏处理的问题时,因为内容较多,所以特整理下来,跟大家分享。有问题,大家评论中交流。
ios 横屏问题大致分为两种 window 级别的控制 和 controller级别的控制
一、windows级别的控制
在appdelegate中如下方法,即可声明应用支持的方向。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window返回参数为
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};这个方法与info.plist 和 targets-general 中对应的设置,功能是一样的。
二、Controller层面的控制。
基本方法有四个
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return toInterfaceOrientation != UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
当然,仅仅把这几个方法写到相应的controller中有些时候是没有用的。
这些特殊情况就是当你使用TabbarController和NavigationController按照如上做法使用的时候。
办法就是继承TabbarController和NavigationController,并添加上述几个方法。
好吧。如果用的是系统的 TabbarController和NavigationController 最好的方法是:“重写”。。。。
本文详细介绍了iOS横屏处理的问题,包括窗口层级和控制器层级的控制方法。重点阐述了如何在AppDelegate中声明应用支持的方向,以及在Controller层面通过四个基本方法实现对界面方向的控制。此外,文章还特别提醒开发者在使用TabbarController和NavigationController时需要注意的特殊情况,建议继承这些控制器并添加相应的方法来解决横屏问题。
5631

被折叠的 条评论
为什么被折叠?



