IOS代码管控APP页面横竖屏切换

IOS如何使用代码管控APP页面横竖屏切换?


这个我写了个小demo,下载链接 http://code4app.com/ios/53c78e77933bf098108b4ea0


1⾸首先是AppDelegate,这⾥里加载rootViewController⽅方法要变⼀下 :

- (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{
    self.window = [[UIWindow alloc]initWithFrame:screenBounds];
    LoadingViewController * mainViewController =[[LoadingViewController alloc] init];
    rootNavigationController * navigationController = [[rootNavigationController alloc]
					initWithRootViewController:mainViewController];
    [window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];
    return YES;


LoadingViewController就是我的⼀一个⻚页⾯面,我让LoadingViewController作为我的主⻚页⾯面。


2 .但是在这之前,我不能直接将mainViewController设置为self.window.rootViewController

中间要拐⼀下,关键就是这个rootNavigationController *navigationController。。。。。 

代码如下:rootNavigationController.h ⽂文件代码

#import 
@interface rootNavigationController : UINavigationController

@end

rootNavigationController.m ⽂文件代码

#import "rootNavigationController.h"
@interface rootNavigationController ()
@end
@implementation rootNavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

}

    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the

view.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be

recreated.

}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often
want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
    // Get the new view controller using [segue
destinationViewController].
    // Pass the selected object to the new view
controller.
}
*/
-(BOOL)shouldAutorotate
{

return YES;}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject
supportedInterfaceOrientations];
}
-
(UIInterfaceOrientation)preferredInterfaceOrientation
ForPresentation
{
    return [self.viewControllers.lastObject
preferredInterfaceOrientationForPresentation];
}

@end 


rootNavigationController这个类其实平平⽆无奇,关键在于下⾯面三个⽅方法:

-(BOOL)shouldAutorotate
{

return YES;}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject
supportedInterfaceOrientations];
}
-
(UIInterfaceOrientation)preferredInterfaceOrientation
ForPresentation
{
    return [self.viewControllers.lastObject
preferredInterfaceOrientationForPresentation];
}


这三个方法就是用来控制所有的页面的横竖屏的;这三个方法是系统级的;

你不写是有另外一套规则;你写上去,自己不调用,也是会运行的.

app启动首先走 main函数,然后main函数调用AppDelegate ,AppDelegate再调用UINavigationController,UINavigationController在调才是你的界面.

UINavigationController  继承的也是UIViewController ;

但他比UIViewController更进一步.

一个app中一般来讲就一个 UINavigationController的实例在运行,所谓的UIViewController 都是在这个UINavigationController 运行的.


而且我们在做view push /pop操作的时候,其实都是在navigationController这个里面做的.

就是这个app全局的唯一实例UINavigationController  

(当然,不自定义这个rootNavigationController类也是可以的嘛,初学者进公司面试的时候,面试官不是经常问这样的问题嘛,如何进行类扩展啊?各位看官自己回想一下咯,我在这里就不再赘述了)


3.当然,你要使用横竖屏幕切换,Device Orientation,上下左右四个方向就不能只勾选一个方向了,当然你要看你的项目所需要支持的方向,来判定,用哪几个点选哪几个。




然后,后面的UIViewController  都可以用代码来控制横竖屏了.


4.当然另外需要注意的是,还要在AppDelegate添加上以下方法:

- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window

{

    // iPhone doesn't support upside down by default, while the iPad does.  Override to allow all orientations always, and let the root view controller decide what's allowed (the supported orientations mask gets intersected).    

    returnUIInterfaceOrientationMaskAll;

}


5.后面的页面控制横竖平方法:


简单来讲直接添加以下方法就好,当然,这是个强制竖屏的例子

-(NSUInteger)supportedInterfaceOrientations{

    returnUIInterfaceOrientationMaskPortrait;

}


- (BOOL)shouldAutorotate

{

    returnNO;

}


-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    returnUIInterfaceOrientationPortrait;

}


每个方法的意思和左右我就不详细解释了,自己去查文档,话说直接直接command+左键也能够看头文件吧,里面注释的也很清楚。


这是个强制右转的例子

- (BOOL)shouldAutorotate

{

    returnNO;

}


-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    returnUIInterfaceOrientationLandscapeRight;

}


-(NSUInteger)supportedInterfaceOrientations

{

    returnUIInterfaceOrientationMaskLandscapeRight;

}


当然,shouldAutorotate是用来控制自动旋转的,你可以设置


6.另外,重要的是,从A(竖屏)切换到B(横屏),不能用ApushViewControllerB,

必须用A presentViewController B

[self.navigationController pushViewController:B animated:YES];(X)

[self presentViewController:B animated:YES completion:Nil];(V)

问题在哪你们自己体会吧。


示例demo,下载链接 http://code4app.com/ios/53c78e77933bf098108b4ea0






你可能感兴趣的:(横屏,切换,竖屏,代码管控)