模態顯示PresentModalViewController


1、主要用途

  • 彈出模態ViewController是IOS變成中很有用的一個技術,UIKit提供的一些專門用於模態顯示的ViewController,如UIImagePickerController等。彈出模態ViewController
  • 主要使用於一下這幾種情形:

    • 1、收集用戶輸入信息
    • 2、臨時呈現一些內容
    • 3、臨時改變工作模式
    • 4、相應設備方向變化(用於針對不同方向分別是想兩個ViewController的情況)
    • 5、顯示一個新的view層級
    • 這幾種情形都會暫時中斷程序正常的執行流程,主要作用是收集或者顯示一些信息。

2、彈出方法:presentModalViewController:

  • 由 視圖控制器類對象調用presentModalViewController: 方法。
  • 以模態窗口的形式管理視圖,當前視圖關閉前其他視圖上的內容無法操作。

3、modalTransitionStyle屬性

  • 通過設置設置presenting VC的modalTransitionStyle屬性,我們可以設置彈出presented VC時場景切換動畫的風格,包含類型如下:
typedef enum {
	UIModalTransitionStyleCoverVertical,   // 自下而上(從底部滑入)
	UIModalTransitionStyleFlipHorizontal,  // 自左至右180度翻轉(水平翻轉進入)
	UIModalTransitionStyleCrossDissolve,   // 淡出效果(交叉溶解)
	UIModalTransitionStylePartialCurl      // 翻頁效果
} UIModalTransitionStyle;
  • 這四種風格在不受設備的限制,即不管是iPhone還是iPad都會根據我們指定的風格顯示轉場效果。

4、Modal Presentation Styles(彈出風格)

  • 通過設置presenting VC的modalPresentationStyle屬性,我們可以設置彈出View Controller時的風格,有以下四種風格,其定義如下:
typedef enum {
	UIModalPresentationFullScreen = 0,
	UIModalPresentationPageSheet,
	UIModalPresentationFormSheet,
	UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
  • 4.1 UIModalPresentationFullScreen

    • 代表彈出VC時,presented VC充滿全屏,如果彈出VC的wantsFullScreenLayout設置為YES的,則會填充到狀態欄下邊,否則不會填充到狀態欄之下。
  • 4.2 UIModalPresentationPageSheet

    • 代表彈出是彈出VC時,presented VC的高度和當前屏幕高度相同,寬度和豎屏模式下屏幕寬度相同,剩余未覆蓋區域將會變暗並阻止用戶點擊,這種彈出模式下,豎屏時跟UIModalPresentationFullScreen的效果一樣,橫屏時候兩邊則會留下變暗的區域。
  • 4.3 UIModalPresentationFormSheet

    • 這種模式下,presented VC的高度和寬度均會小於屏幕尺寸,presented VC居中顯示,四周留下變暗區域。
  • 4.4 UIModalPresentationCurrentContext

    • 這種模式下,presented VC的彈出方式和presenting VC的父VC的方式相同。
  • 這四種方式在iPad上面統統有效,但在iPhone和iPod touch上面系統始終已UIModalPresentationFullScreen模式顯示presented VC。

5、關閉方法:dismissModalViewControllerAnimated:

  • 由 視圖控制器類對象調用dismissModalViewControllerAnimated: 方法。

6、獲取不同的模態窗口(主要的屬性)

@property(nonatomic, readonly) UIViewController *presentedViewController ; // 當前控制器模態出的窗口
@property(nonatomic, readonly) UIViewController *presentingViewController; // 模態出當前控制器的窗口

7、處理模態窗口(主要的方法)

// 顯示想要顯示的模態窗口
- (void)presentViewController:(UIViewController *)viewControllerToPresent 
					 animated: (BOOL)flag 
				   completion:(void (^)(void))completion);

// 關閉當前顯示的模態窗口				   
- (void)dismissViewControllerAnimated: (BOOL)flag 
                           completion: (void (^)(void))completion);

// 當前控制器模態另一個窗口並傳輸數據時調用的方法                           
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)

8、使用提醒

presenting view controller Vs presented view controller

  • 當我們在view controller A中模態顯示view controller B的時候,A就充當presenting view controller(彈出VC),而B就是presented view controller(被彈出VC)。官方文檔建議這兩者之間通過delegate實現交互,如果使用過UIImagePickerController從系統相冊選取照片或者拍照,我們可以發現imagePickerController和彈出它的VC之間就是通過UIImagePickerControllerDelegate實現交互的。因此我們在實際應用用,最好也遵守這個原則,在被彈出的VC中定義delegate,然后在彈出VC中實現該代理,這樣就可以比較方便的實現兩者之間的交互。

9、示例

ModalTestController *vc = [[ModalTestController alloc] init];

vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
vc.hidesBottomBarWhenPushed = YES;
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
navi.definesPresentationContext = YES;
navi.modalPresentationStyle = UIModalPresentationCurrentContext;

[self.navigationController presentViewController:navi animated:YES completion:^{
	navi.navigationBarHidden = YES;
}];


免責聲明!

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



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