关于ios里webview的orientation控制的一些小结

来源:互联网 发布:软件著作权 受理登记 编辑:程序博客网 时间:2024/04/30 16:39

在app里打开H5应用,产品希望能进入全屏模式,并且能指定orientation(方向),比如选定以横屏或者竖屏打开。

一、如何在打开View Controller时指定其初始方向?

ios6及以后建议的方式是:

1、以present(presentViewController)全屏方式打开VC(view controller,一般来说是UINavigationController、UITabBarController的子类),此VC被称之根视图(root VC)

2、在根视图里实现方法- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation,返回选定的orientation

 

其他可能的办法及风险:

1、如果用push(pushViewController)的方式打开全屏VC,可以用setTransform来旋转视图,但是有风险(特别地,如果被旋转的视图切换到其它视图且再切回来,之前的setTransform方法可能会工作不正常,因为第一次使用setTransform时使用了特征矩阵,再次使用时,此矩阵已经被改变);

2、使用私有方法setOrientation,此方法可以强制指定设备方向,但是可能会导致app审核时被Apple拒绝;

 

二、如何控制旋转?

ios6及之后的方式是:

1、在最顶层(topmost)的VC或者present的根视图里,实现如下两个方法(注意,在其他视图里实现无效):

shouldAutorotate:return Yes/No,指明视图是否允许旋转

supportedInterfaceOrientations:return想支持的orientations

 

事件触发顺序是:当设备发生旋转时,会先调用shouldAutorotate再调用supportedInterfaceOrientations来判断是否进行VC旋转,如果前者返回No,后者不会被调用。在第一次加载VC时,也会调用supportedInterfaceOrientations,所以如果VC只支持一个方向,那么在supportedInterfaceOrientations里返回那个方向即可,这种情况下不用再实现preferredInterfaceOrientationForPresentation。另外,preferredInterfaceOrientationForPresentation返回的方向必须是supportedInterfaceOrientations里支持的方向,否则会导致一个运行时error。preferredInterfaceOrientationForPresentation会在加载VC时调用,且在调用supportedInterfaceOrientations之前。

 

三、ios5及之前的方式

ios5及之前的控制比较简单,在VC里重载一个方法- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation即可,返回此VC是否支持指定的orientation(此入参标明旋转后的UI方向),Yes表示支持。

0 0
原创粉丝点击