参考总结1的内容发现在Ios78中,覆盖supportedInterfaceOrientations方法无效。正确的做法是覆盖以下方法:
自定义一个UINavigationController的子类,当添加viewcontroller的时候记录当前显示的那个viewcontroller,然后重写preferredInterfaceOrientationForPresentation和shouldAutorotate使之调用当前显示的viewcontroller里面的屏幕旋转方法
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
UIViewController* viewController = [self.viewControllersobjectAtIndex:index];
return viewController.preferredInterfaceOrientationForPresentation;
}
-(BOOL)shouldAutorotate {
UIViewController* viewController = [self.viewControllersobjectAtIndex:index];
return viewController.shouldAutorotate;
}
最后你需要在每一个需要改变屏幕方向的viewcontroller里面重写preferredInterfaceOrientationForPresentation和shouldAutorotate,让它选择正确的旋转方向就行
ios8通过preferredInterfaceOrientationForPresentation锁定的旋转,弹出UIActionSheet的时候会出现显示方向不对的问题,我们可以在appdelegate里面给出正确的方向
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSString* strClass = [NSStringstringWithUTF8String:object_getClassName(window.rootViewController)];
if ([@"_UIAlertShimPresentingViewController"isEqualToString:strClass]) {
returnUIInterfaceOrientationMaskPortrait;
}
return [applicationsupportedInterfaceOrientationsForWindow:window];
}
ios8需要正确的显示软键盘的方向需要设置如下:
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];