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 <UIKit/UIKit.h>
@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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是iOS平台横竖切换的详细代码: 1. 在项目的Info.plist文件中添加以下代码: ``` <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> ``` 其中,UIInterfaceOrientationPortrait表示竖方向,UIInterfaceOrientationLandscapeLeft表示横向左方向,UIInterfaceOrientationLandscapeRight表示横向右方向; 2. 在AppDelegate.m文件中添加以下代码: ``` - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (self.allowRotation) { return UIInterfaceOrientationMaskAll; } else { return UIInterfaceOrientationMaskPortrait; } } ``` 其中,allowRotation是一个BOOL类型的变量,用于制是否允许横竖切换; 3. 在需要进行横竖切换的场景中,添加以下代码: ``` - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; } ``` 其中,shouldAutorotate返回YES表示允许自动旋转,supportedInterfaceOrientations返回UIInterfaceOrientationMaskAll表示支持所有方向的旋转。 需要注意的是,以上的代码只是最基本的配置,如果需要更加详细的配置,可以参考苹果官方文档中的相关说明。同时,不同版本的iOS可能会有所不同,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值