首页 > 技术文章 > 如何实现ios屏幕的横竖屏自适应

4Dream 2015-06-23 17:19 原文

首先在UIViewController的
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
方法中设置设备要支持的deviceOrientation;
在UIViewController的
-(void)willRotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation 
duration:(NSTimeInterval)duration
或者
-(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
duration:(NSTimeInterval)duration
重新设置所有子view的frame
 
当UIView设置成自动适配屏幕(即myView.autoresizesSubviews = YES)时,当我们重新设置myView的frame时(一般屏幕旋转时我们都会重新设置view的frame),会自动调用layoutSubviers方法,我们可以在该方法中判断屏幕的方向,并调整各子view的frame。
 
如果工层中有一个superViewController,然后在该控制器的试图上加上了另一个subViewController的view,无论如何subViewController的以上三个方法都不会被调用,这个时候,可以在superViewController的
-(void)willRotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation 
duration:(NSTimeInterval)duration
或者
-(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
duration:(NSTimeInterval)duration
方法中重新配置subViewController的view。

 

 

推荐阅读