代碼處理iOS的橫豎屏旋轉
本文為CocoaChina網友南華coder投稿
前文鏈接:代碼處理iOS的橫豎屏旋轉(上)
三、屏幕旋轉方向下的視圖處理
1、屏幕旋轉時,建議監聽UIApplicationDidChangeStatusBarOrientationNotification
原因1:supportedInterfaceOrientations方法中最終返回的是多個界面方向。
原因2(最重要的原因):我們真正要處理的是頁面方向發生旋轉UI的變化。而在設備的物理方向發生旋轉的時候,如果此時當前控制器的頁面並沒有旋轉,我們這時改變UI布局,可能就發生問題了。
2、屏幕的寬高處理
1)在iOS 8之後,當屏幕旋轉的時候,[[UIScreen mainScreen] bounds]也發生了改變。如橫屏時候的屏幕寬度 其實是豎屏的時候屏幕的高度。
2)我們處理視圖布局時候,如果使用到屏幕的寬高,不要直接使用SCREEN_HEIGHT和SCREEN_WIDTH,而使用SCREEN_MIN和SCREEN_MAX
#define SCREEN_HEIGHT CGRectGetHeight([[UIScreen mainScreen] bounds]) #define SCREEN_WIDTH CGRectGetWidth([[UIScreen mainScreen] bounds]) #define SCREEN_MIN MIN(SCREEN_HEIGHT,SCREEN_WIDTH) #define SCREEN_MAX MAX(SCREEN_HEIGHT,SCREEN_WIDTH)
說明:豎屏時候,寬是SCREEN_MIN,高是SCREEN_MAX;橫屏時候,寬是SCREEN_MAX,高是SCREEN_MIN。
3、屏幕旋轉下處理Demo
//監聽UIApplicationDidChangeStatusBarOrientationNotification的處理 - (void)handleStatusBarOrientationChange: (NSNotification *)notification{ UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; BOOL isLandscape = NO; switch (interfaceOrientation) { case UIInterfaceOrientationUnknown: NSLog(@"未知方向"); break; case UIInterfaceOrientationPortrait: case UIInterfaceOrientationPortraitUpsideDown: isLandscape = NO; break; case UIInterfaceOrientationLandscapeLeft: case UIInterfaceOrientationLandscapeRight: isLandscape = YES; break; default: break; } if (isLandscape) { self.tableView.frame = CGRectMake(0, 0, SCREEN_MAX, SCREEN_MIN - 44); }else{ self.tableView.frame = CGRectMake(0, 0, SCREEN_MIN, SCREEN_MAX - 64); } [self.tableView reloadData]; }
說明:當然也可以選擇使用Masonry這樣優秀的AutoLayout布局第三方庫來處理,storyBoard來布局次之。
4、屏幕旋轉下處理Demo效果圖
豎屏下效果.png
橫屏下效果.png
5、屏幕旋轉處理的建議
1)旋轉前後,view當前顯示的位置盡量不變
2)旋轉過程中,暫時界面操作的響應
3)視圖中有tableview的話,旋轉後,強制 [tableview reloadData],保證在方向變化以後,新的row能夠充滿全屏。
四、強制橫屏
APP中某些頁面,如視頻播放頁,一出現就要求橫屏。這些橫屏頁面或模態彈出、或push進來。
1、模態彈出ViewController情況下 強制橫屏的設置//QSShow3Controller.m - (BOOL)shouldAutorotate{ return NO; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskLandscapeRight; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationLandscapeRight; } //模態彈出 QSShow3Controller *vc = [[QSShow3Controller alloc]init]; [self presentViewController:vc animated:YES completion:nil];
說明:這種情況比較簡單處理。
2、push推入ViewController情況下 強制橫屏的設置
//QSShow4Controller.m -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self setInterfaceOrientation:UIInterfaceOrientationLandscapeRight]; } //強制轉屏(這個方法最好放在BaseVController中) - (void)setInterfaceOrientation:(UIInterfaceOrientation)orientation{ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; // 從2開始是因為前兩個參數已經被selector和target佔用 [invocation setArgument:&orientation atIndex:2]; [invocation invoke]; } } //必須返回YES - (BOOL)shouldAutorotate{ return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskLandscapeRight; } //Push推入 QSShow4Controller *vc = [[QSShow4Controller alloc]init]; [self.navigationController pushViewController:vc animated:YES];
說明:蘋果不允許直接調用setOrientation方法,否則有被拒的風險;使用NSInvocation對象給[UIDevice currentDevice]發消息,強制改變設備方向,使其頁面方向對應改變,這是蘋果允許的。
五、其他
1、 APP啟動時,手機橫屏下,首頁UI(該頁面只支持豎屏)出錯(add by 2017/6/20)
//設置設置狀態欄豎屏 [[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationPortrait];
以上詳細源碼參考:QSRotationScreenDemo


※npx:npm 5.2.0 內置的包執行器
※滴滴插件化方案 VirtualApk 源碼解析
※深入探索JVM自動資源管理
※盤點國外多個APPStore應用屏幕截圖示例
※webpack2終極優化
TAG:推酷 |
※開源RISC-V架構正在改變IoT處理器的遊戲規則
※一窺ARM的AI處理器
※一窺ARM的AI處理器
※Intel正式修復幽靈漏洞 開始提供部分處理器BIOS更新
※AMD正式發布新一代銳龍Pro 處理器
※PS—超簡單的LOMO照片處理
※Sony 與微軟下一代遊戲機處理器將採用 AMD Ryzen 處理器
※AMD確認二代「線程撕裂者」處理器流片
※OPPO R15配置被徹底扒光 處理器和相機CMOS雙首發
※Intel酷睿第八代移動處理器偷跑
※TIPS 泥鰍處理方法
※新一代MacBook曝光:處理器升級
※改變iOS 下一個十年?搭載ARM處理器Mac或將至
※一行代碼,Pandas秒變分散式,快速處理TB級數據
※AMD FX系列處理器的興衰
※Intel正式發布八代酷睿移動處理器
※手機數碼 為什麼OPPO和VIVO的手機處理器很差,但是看起來很流暢呢?
※OPPO R1變革時代,高端處理器搶先看!
※MacBook Pro 更新了處理器和屏幕,鍵盤進灰問題還沒解決
※iPhone X屏幕出現綠線,該如何處理