如何實現ios屏幕的橫豎屏自適應


首先在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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM