强制旋转一个UIViewController

刚才V2ex上问了这么一个问题: http://www.v2ex.com/t/97577#reply7

然后就随手搜了下 stackoverflow

在iOS5的年代是有这么个API:

1
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationPortrait]; 

但是在iOS 6开始,苹果把这个API 私有了。

于是乎,可以用OC runtime 绕过去,但是:

有可能审核会被拒掉

做法就是用 objc/message.h 强制对这个类发消息

1
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), @(UIInterfaceOrientationPortrait)); 

或者

1
2
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait)  forKey:@"orientation"]; 

都可以达到同样的效果。

这个bug的原因

其实,这不应该叫做bug的,应该说是开发者不理解iOS 设备的屏幕旋转 那三个API的触发条件。

触发条件仅有两个:

  • 设备方向改变
  • 当rootViewController 改变的时候

于是乎,一直在push pop是不会触发rootViewController的旋转检测的。 这个时候只能用各种怪招了,这里还有一个不用私有API也能实现的作法:

在viewWillAppear里面加上这东西:

1
2
3
4
5
[self presentViewController:[UIViewController new]  animated:NO  completion:^{  [self dismissViewControllerAnimated:NO completion:nil];  }];

转载于:https://www.cnblogs.com/UncleJoke/p/3977835.html

猜你喜欢

转载自blog.csdn.net/weixin_34334744/article/details/94317492
今日推荐