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

来源:互联网 发布:图片 it 软件测试 编辑:程序博客网 时间:2024/05/16 01:18

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 <UIKit/UIKit.h>@interface rootNavigationController : UINavigationController

@end

rootNavigationController.m ⽂文件代码

#import "rootNavigationController.h"@interface rootNavigationController ()@end
@implementation rootNavigationController
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(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 oftenwant to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)seguesender:(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)preferredInterfaceOrientationForPresentation{
    return [self.viewControllers.lastObjectpreferredInterfaceOrientationForPresentation];}

@end 


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

-(BOOL)shouldAutorotate{

return YES;}

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


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

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

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

0 0
原创粉丝点击