最近做ipad項目,遇到不少螢幕轉屏發生的錯誤現象。(其中有些還是自己編碼時的疏忽和不規範導致的)
那以下就是總結一些做支援旋轉的時候的一些思路和碰到的問題時如何解決的。
首先描述 下工程的大體的一個結構特徵。
工程是以UISplitViewController 為依託,然後它的viewControllers分別是 UITabBarController 和 UINavigationController。其中UITabBarController裡面的viewControllers又分別是一個個UINavigationController組成。
具體詳見圖1豎屏
圖2橫屏
首先這邊碰到一個問題是在橫屏的時候要是沒有對處理UITabBarController進行處理那麼會出現在橫屏啟動程式的時候第一個UINavigationController會向下降低20像素的現象
詳見圖3
導致這個現象現在暫時的一個處理是在建立UITabBarController的時候先tabBarCtr.selectedIndex = 1;
然後在方法
- (void)viewDidLoad
{
tabBarCtr.selectedIndex = 0;
}
這樣就可以暫時解決掉橫屏顯示異常的現象。(到時候在找到具體解決方案的時候在更新)
以上這邊就是程式的大體的一個組成結構,下面進入到我們正式的旋轉螢幕的時候是如何支援的。
以下針對的是主ctr 的操作即 UISplitViewController
旋轉螢幕其實就是要管理好 1、UIViewController 2、添加在UIViewController上的view或者單獨的一些view的處理控制。
首先對應UIViewController它裡面提供了很多旋轉時候的一些代理和方法。
1、設定支援自動適應橫豎屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
2、在螢幕快要發生改變的時候進行處理
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//其實我們在這邊要做的就是傳遞旋轉訊息和對view做相應的改變
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++)
{
//傳遞旋轉的訊息到UITabBarController底下的UIViewController
UIViewController *viewCtr = (UIViewController *)[ctrs objectAtIndex:j];
[viewCtr willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if ([viewCtr.view respondsToSelector:@selector(reloadSubviews)])
{
//對UIViewController進行重新重新整理view的位置的操作
//reloadSubviews方法就是在每個ctr類中實現對ctr 上view重新布局的操作
[viewCtr.view performSelector:@selector(reloadSubviews)];
}
}
[navCtr willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
//以上這種方式就已經對UIViewController和其上的view進行了旋轉的相應操作
//個人覺得當然這裡也可以用通知進行訊息的傳遞
}
/////////////////////////////////////////////////////////////////
在某個ctr中 的 reloadSubviews方法範例
- (void)reloadSubviews
{
CGRect frame = getScreenRect();//用來擷取當前旋轉後螢幕的大小 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旋轉的時候如果在橫屏的時候對UIViewController 進行push多層的時候出現異常(push後退出的動作本來是從右向左的展示,但是怪象就是退出的時候變成
從上到下的操作)
其實這個時候要去檢查下你push的UIViewController 中對shouldAutorotateToInterfaceOrientation設定是否為與上層的ctr方法返回一致
如上層返回
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
那麼你這邊也應當是YES
*************************************************************