iPad 和 iPhone 屏幕旋轉(zhuǎn)支持 圖文詳解
iPad 和 iPhone 屏幕旋轉(zhuǎn)支持 圖文詳解是本文要介紹的內(nèi)容。最近做ipad項目,遇到不少屏幕轉(zhuǎn)屏發(fā)生的錯誤現(xiàn)象。(其中有些還是自己編碼時的疏忽和不規(guī)范導(dǎo)致的)
那以下就是總結(jié)一些做支持旋轉(zhuǎn)的時候的一些思路和碰到的問題時如何解決的。
首先描述 下工程的大體的一個結(jié)構(gòu)特征。
工程是以UISplitViewController 為依托,然后它的viewControllers分別是 UITabBarController 和 UINavigationController。其中UITabBarController里面的viewControllers又分別是一個個UINavigationController組成。
具體詳見圖1豎屏
圖2橫屏
首先這邊碰到一個問題是在橫屏的時候要是沒有對處理UITabBarController進行處理那么會出現(xiàn)在橫屏啟動程序的時候***個UINavigationController會向下降低20像素的現(xiàn)象
詳見圖3
導(dǎo)致這個現(xiàn)象現(xiàn)在暫時的一個處理是在創(chuàng)建UITabBarController的時候先tabBarCtr.selectedIndex = 1;
然后在方法
- (void)viewDidLoad
- tabBarCtr.selectedIndex = 0;
這樣就可以暫時解決掉橫屏顯示異常的現(xiàn)象。(到時候在找到具體解決方法的時候在更新)
以上這邊就是程序的大體的一個組成結(jié)構(gòu),下面進入到我們正式的屏幕旋轉(zhuǎn)的時候是如何支持的。
以下針對的是主ctr 的操作即 UISplitViewController
屏幕旋轉(zhuǎn)其實就是要管理好 1、UIViewController 2、添加在UIViewController上的view或者單獨的一些view的處理控制。
首先對應(yīng)UIViewController它里面提供了很多旋轉(zhuǎn)時候的一些代理和方法。
1、設(shè)置支持自動適應(yīng)橫豎屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return YES;
- }
2、在屏幕快要發(fā)生改變的時候進行處理
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- {
- //其實我們在這邊要做的就是傳遞旋轉(zhuǎn)消息和對view做相應(yīng)的改變
- for (int i=0; i<[tabBarCtr.viewControllers count]; i++)
- {
- UINavigationController *navCtr = (UINavigationController *)[tabBarCtr.viewControllers objectAtIndex:i];
- NSArray *ctrs = navCtr.viewControllers;
- for (int j=0; j<[ctrs count]; j++)
- {
- //傳遞旋轉(zhuǎn)的消息到UITabBarController底下的UIViewController
- UIViewController *viewCtr = (UIViewController *)[ctrs objectAtIndex:j];
- [viewCtr willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
- if ([viewCtr.view respondsToSelector:@selector(reloadSubviews)])
- {
- //對UIViewController進行重新刷新view的位置的操作
- //reloadSubviews方法就是在每個ctr類中實現(xiàn)對ctr 上view重新布局的操作
- [viewCtr.view performSelector:@selector(reloadSubviews)];
- }
- }
- [navCtr willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
- }
- //以上這種方式就已經(jīng)對UIViewController和其上的view進行了旋轉(zhuǎn)的相應(yīng)操作
- //個人覺得當(dāng)然這里也可以用通知進行消息的傳遞
- }
在某個ctr中 的 reloadSubviews方法樣例
- (void)reloadSubviews
- {
- CGRect frame = getScreenRect();//用來獲取當(dāng)前旋轉(zhuǎn)后屏幕的大小 frame就是為刷新提供大小
- AA.frame = CGRectMake(frame.size.width-140, 104, 120, 40);
- BB.frame = CGRectMake(frame.size.width-140, 44, 120, 40);
- Ctr.view.frame = CGRectMake(0, 200, frame.size.width, frame.size.height-200);
- [tableView reloadData];
- }
ipad旋轉(zhuǎn)的時候如果在橫屏的時候?qū)IViewController 進行push多層的時候出現(xiàn)異常(push后退出的動作本來是從右向左的展示,但是怪象就是退出的時候變成從上到下的操作)
其實這個時候要去檢查下你push的UIViewController 中對shouldAutorotateToInterfaceOrientation設(shè)置是否為與上層的ctr方法返回一致
如上層返回
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return YES;
- }
那么你這邊也應(yīng)當(dāng)是YES
小結(jié):關(guān)于iPad 和 iPhone 屏幕旋轉(zhuǎn)支持 圖文詳解的內(nèi)容介紹完了希望本文對你有所幫助!