某一个页面需要横屏,其他的页面任然保持竖屏需要以下关键的几个步骤:

1.修改系统代理方法的返回值

 -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
//需要横屏的页面将此属性allowRotation修改为YES,竖屏的页面修改为NO
if (_allowRotation == YES) {
//页面仅支持横屏
return UIInterfaceOrientationMaskLandscapeRight;
}else{
//页面仅支持竖屏
return UIInterfaceOrientationMaskPortrait; }
}

2.在需要横屏的界面修改方法-(BOOL)shouldAutorotate的返回值为YES

- (BOOL)shouldAutorotate {

#if 1

    // 设置orientation来横屏竖屏(方式一)
return YES; #else // 旋转式横屏竖屏(方式二)
return NO; #endif }

3.代码实现将屏幕横过来

#pragma mark 横竖屏切换方法
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
/// 注释掉是因为包含私有API,不敢调用啊
// if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
//
// SEL selector = NSSelectorFromString(@"setOrientation:");
// NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
// [invocation setSelector:selector];
// [invocation setTarget:[UIDevice currentDevice]];
// int val = orientation;
// // 从2开始是因为0 1 两个参数已经被selector和target占用
// [invocation setArgument:&val atIndex:2];
// [invocation invoke];
// } #if 1 // 设置orientation来横屏竖屏(方式一)
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"]; NSNumber *orientationTarget = [NSNumber numberWithInt:orientation]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
//刷新
[UIViewController attemptRotationToDeviceOrientation]; #else // 旋转式横屏竖屏(方式二)
if (orientation == UIInterfaceOrientationLandscapeRight) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
self.view.transform = CGAffineTransformMakeRotation(M_PI/);
self.view.frame = CGRectMake(, , MainScreenHeight, MainScreenWidth);
self.titleImage.frame = CGRectMake(, , MainScreenHeight, MainScreenWidth);
} if (orientation == UIInterfaceOrientationPortrait) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
self.view.transform = CGAffineTransformMakeRotation();
self.view.frame = CGRectMake(, , MainScreenWidth, MainScreenHeight);
self.titleImage.frame = CGRectMake(, , MainScreenWidth, MainScreenHeight);
} #endif
}

4.UIViewController系统代理方法来检测屏幕旋转

#pragma mark 横竖屏切换或者进来横屏会触发的方法
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
//转屏前调入 -- 此处调用在viewDidLoad之前(重写init方法除外),可在此处做一些控件布局的横竖屏转换
completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
//转屏后调入 -- 此处调用在viewDidLoad之后,可在此处做一些控件布局的横竖屏转换
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

经过上述4个步骤可完美实现转屏

by:初光夫

ios开发某个页面横不过来屏幕的时候的更多相关文章

  1. iOS开发学习-放大长图与屏幕等宽

    /* 需要得到一个图片的放大比例,这个比例就是屏幕的宽度与图片真实宽度的比值 */ CGFloat newZoomScale = LZ_SCREEN_WIDTH / [_photoImageView. ...

  2. iOS开发 个别页面是否支持页面旋转

    - (BOOL)shouldAutorotate { return YES; }

  3. iOS开发解决页面滑动返回跟scrollView左右划冲突

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithG ...

  4. iOS开发-委托(Delegate)浅谈

    委托其实并不是OC中才有,C#中也有,不过彼此的理解方式是不一样的,OC中委托是协议的一种,需要使用@protocol声明,委托一般在iOS开发中页面中传值用的比较多.委托是Cocoa中最简单.最灵活 ...

  5. IOS开发之绝对布局和相对布局(屏幕适配)

    之前如果做过Web前端页面的小伙伴们,看到绝对定位和相对定位并不陌生,并且使用起来也挺方便.在IOS的UI设计中也有绝对定位和相对定位,和我们的web前端的绝对定位和相对定位有所不同但又有相似之处.下 ...

  6. iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换

    iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换 不多说直接上效果图和代码 1.设置RootViewController为一个导航试图控制器 //  Copyright © 2016年 ...

  7. 谈谈iOS开发如何写个人中心这类页面--静态tableView页面的编写

    本文来自 网易云社区 . 一.本文讲的是什么问题? 在开发 iOS 应用时,基本都会遇到个人中心.设置.详情信息等页面,这里截取了某应用的详情编辑页面和个人中心页面,如下: 我们以页面结构的角度考虑这 ...

  8. iOS开发点滴:iPhone屏幕适配

    最近开始做iOS开发,遇到一些小问题和解决方法,记录下.   今天是iPhone屏幕适配 iPhone5出来之后屏幕就有iPhone就有了2种尺寸:3.5寸和4寸,xcode 5 的IB设计器里面界面 ...

  9. iOS开发之各种动画各种页面切面效果

    因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一些干货,切勿错过哦.今天所介绍的主题是关于动画的,在之前的博客中也有用到动画的地方,今天就好好的总结一下iOS开发中常用的动画.说道动画其 ...

随机推荐

  1. (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中

    本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...

  2. Sql日期时间格式转换(转 子夜.)

    sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...

  3. loj2055 「TJOI / HEOI2016」排序

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  4. C#入门篇6-4:字符串操作 string分割字符串效率比较

    //分割字符串效率比较 public static void Fund() { //1.用string.Split方法 //a.字节数组: //625毫秒/百万次 string str1 = &quo ...

  5. 设计模式之第16章-代理模式(Java实现)

    设计模式之第16章-代理模式(Java实现) “现在朋友圈真是太让人蛋疼了啊.”“怎么说?”“一堆代理,各种卖东西的,看着好烦人.”“哎,删了呗.”“都是朋友,哪里好意思删啊.”“这倒也是...哎,迫 ...

  6. Selenium中如何运行 auto.exe 文件

    Runtime exe = Runtime.getRuntime(); try{ String str = "D:\\Auto上传文件\\photo.exe"; exe.exec( ...

  7. hnust hold不住的老师

    问题 H: Hold不住的老师 时间限制: 1 Sec  内存限制: 128 MB提交: 415  解决: 63[提交][状态][讨论版] 题目描述 因为我们学校ACM集训队取得的一个个优异成绩,AC ...

  8. manjaro无声音

    解决方法:https://forum.manjaro.org/t/no-sound-solved/3517 LOL i feel like such a noob. Fixed my problem ...

  9. MyEclipse断点调试方法

    MyEclipse断点调试方法 最基本的操作是: 1, 首先在一个java文件中设断点,然后运行,当程序走到断点处就会转到debug视图下, 2, F5键与F6键均为单步调试,F5是step into ...

  10. BZOJ1562 [NOI2009]变换序列 【KM算法】

    题目 输入格式 输出格式 输入样例 5 1 1 2 2 1 输出样例 1 2 4 0 3 提示 30%的数据中N≤50: 60%的数据中N≤500: 100%的数据中N≤10000. 题解 每个位置可 ...