iOS中单个页面横竖屏切换 点击退出再退回到竖屏。

在开发项目的时候,遇到了一个问题,就是其中一个页面需要强制横屏,而其他页面要强制竖屏,然后返回在回到横屏,总结了一些人的经验给需要的人。


首先在AppDelegate.h里面添加@property(nonatomic,assign)NSInteger allowOrientations; 然后实现下面的方法。

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

if(_allowOrientations ==1) {

return UIInterfaceOrientationMaskLandscapeRight;

}

else{

return (UIInterfaceOrientationMaskPortrait);

}

}

最后在需要使用横竖屏切换的控制器导入Appdelegate.h文件 实现方法:

- (void)viewDidLoad{

  [superviewDidLoad]; 

  AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate; 

  appDelegate.allowRotation =1;

}


返回到上一个页面自动切换到竖屏:

if([[UIDevicecurrentDevice] respondsToSelector:@selector(setOrientation:)]) {   

SEL selector =NSSelectorFromString(@"setOrientation:");

NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];    [invocation setSelector:selector];  

[invocation setTarget:[UIDevicecurrentDevice]];

intval =UIInterfaceOrientationPortrait;  

[invocation setArgument:&val atIndex:2];

 [invocation invoke];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterface...
    彬至睢阳阅读 7,294评论 1 6
  • iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...
    零度_不结冰阅读 6,648评论 0 0
  • 总体方向两点: model下,present方式推出界面。 push横屏,带tabbar、navigation,且...
    sfandy阅读 46,702评论 90 99
  • 1、IOS8之后有的方法写到类里强制横屏之后已经没有用了 -(BOOL)shouldAutorotate{ ret...
    RmondJone阅读 4,814评论 0 0
  • 虽然我们养狗的都很爱护自己家的狗狗,但总会有疏忽大意的时候,会让自己家的狗狗生气。有的主人可能认为狗狗生气...
    fd2a171d9f4b阅读 3,928评论 0 0

友情链接更多精彩内容