iOS 屏幕横竖屏适配

首先把项目中的横竖屏打开如下图:

iOS 屏幕横竖屏适配_第1张图片


iOS 屏幕横竖屏适配_第2张图片

(我是[self topViewController]的传送门)

//判断哪个VC 需要横竖屏

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

if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"FormViewController"]) {

return UIInterfaceOrientationMaskAll;

}else if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"ZCFViewController"]){

return UIInterfaceOrientationMaskAll;

}else if ([NSStringFromClass([[self topViewController]class]) isEqualToString:@"ProjectViewController"]){

return UIInterfaceOrientationMaskAll;

}else if ([NSStringFromClass([[self topViewController]class]) isEqualToString:@"CashJournalViewController"]){

return UIInterfaceOrientationMaskAll;

}

return UIInterfaceOrientationMaskPortrait;

}

然后在你想要横竖屏的VC里面监听一个系统的通知就可以了

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(handleDeviceOrientationDidChange:)

name:UIDeviceOrientationDidChangeNotification

object:nil

];

//实现监听到通知的方法

#pragma mark 接收屏幕旋转的通知处理

- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation

{

//此处代码创建的subView 需要重新设置frame ,xib或者StoryBoard 创建的不需要重新设置

//这个方法里面 self.view 的width和height 会改变,但是代码创建的subview的子view的frame,此处不会自动适配

//此处重新设置一下 frame

CGRect frame  = self.FormView.frame;

frame = self.view.frame;

self.FormView.frame = frame;

//也可以做其他想要在横屏的时候做的事

}

你可能感兴趣的:(iOS 屏幕横竖屏适配)