天天看點

IOS的模态視窗(modal)

在iOS開發中,除了使用push方法切換控制器以外,modal也可以實作界面切換,使用modal友善快捷,任何控制器都可以使用modal展示出來,開發中在設定注冊,購物車,點贊等小功能的時候可以使用。

   首先我們簡單了解下ViewController之間的跳轉      
1、如果在 Storyboard中目前的 ViewController和要跳轉的ViewController之間的segue存在,則可以執行performSegueWithIdentifier:sender:這個方法實作跳轉。
2、如果目标ViewController存在Storyboard中,但是沒有segue。你可以通過UIStoryboard的instantiateViewControllerWithIdentifier:這個方法擷取到它,然後再用你想要的方式實作跳轉,如:壓棧      
    由于在iOS中并沒有專門的模态視窗類,模态視窗(modal)在iOS中隻是視圖控制器顯示的一種方式,模态視窗不依賴于控制器容器(比如UITabBarController和UINavigationController),通常用于顯示獨立的内容,在模态視窗顯示的時其他視圖的内容無法進行操作,通俗的講,modal最常用的場景,新的場景完全蓋住了舊的那個。使用者無法再與上一個場景互動,除非他們先關閉這個場景。      
   modal的基本使用方法有如下兩種:      
//使用modal彈出控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
//關閉當初Modal出來的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;
           
      系統自帶的modal動畫效果是從下往上的,但是我們可以通過modalTransitionStyle屬性,實作其他效果。      
typedef enum {  
     UIModalTransitionStyleCoverVertical = 0,  
     UIModalTransitionStyleFlipHorizontal,  
     UIModalTransitionStyleCrossDissolve,    
     UIModalTransitionStylePartialCurl,  
 } UIModalTransitionStyle;
           
我們可以通過UIModalPresentationStyle屬性定義彈出風格      
UIModalPresentationFullScreen充滿全屏,對于IOS7以後版本,如果彈出視圖控制器的wantsFullScreenLayout設定為YES的,則會填充到狀态欄下邊,否則不會填充到狀态欄之下。      
UIModalPresentationPageSheet高度和目前螢幕高度相同,寬度和豎屏模式下螢幕寬度相同,剩餘未覆寫區域将會變暗并阻止使用者點選,這種彈出模式下,豎屏時跟 UIModalPresentationFullScreen的效果一樣,橫屏時候兩邊則會留下變暗的區域。      
UIModalPresentationFormSheet高度和寬度均會小于螢幕尺寸,居中顯示,四周留下變暗區域。      
UIModalPresentationCurrentContext這種模式下,彈出視圖控制器的彈出方式和它的父VC的方式相同。      
 同樣,我們也可以自定義跳轉動畫效果,代碼如下。      
CATransition *animation = [CATransition animation];  
[animation setDuration:0.3];  
[animation setType:kCATransitionPush];  
[animation setSubtype:kCATransitionFromLeft];  
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];  
[[myViewController.view layer] addAnimation:animation forKey:@"SwitchToView"];    
[self presentModalViewController:myViewController animated:YES];
           
 UIModalPresentationFormSheet風格的子產品視窗,如果有輸入框,整個視圖會根據鍵盤的顯隐自動調整。而且整個子產品視窗的大小是可以設定的(greenViewController .view.superview.bounds = CGRectFromString(framePortrait))。如果子產品視窗在顯示時自動定位光标到文本框,例如:- (void)viewDidAppear:(BOOL)animated { [contentTextView becomeFirstResponder];}      
此時子產品視圖已經根據鍵盤顯示調整到正确的位置,可能由于我們在之前設定子產品視窗的bounds      
GreenViewController *greenViewController = [[GreenViewController alloc] init];

greenViewController .modalPresentationStyle = UIModalPresentationFormSheet;

[self presentModalViewController:greenViewController animated:YES];

 greenViewController .view.superview.bounds = CGRectFromString(framePortrait);
           
     這樣子產品視窗會在螢幕中間顯示,我們可以輸入相應内容。      
在開發中會遇到這種情況,要先用presentModalViewController到登入界面,在登入界面在pushViewController到注冊界面,push不過去      
//先使用modal,present出一個登陸界面
 LoginViewController *login = [[LoginViewController alloc]init];  
[self.navigationController presentModalViewController:login animated:YES];   
//從登陸界面push到注冊界面注冊界面沒有效果  
RegisterViewController *registerViewConotroller = [[RegisterViewController alloc]init];  
 [self.navigationController pushViewController:registerViewConotroller animated:YES]; 
           
      此時就要在push之前自定義UINavigationController,将其跟控制器設定為registerViewConotroller,代碼如下:      
LoginViewController *login = [[LoginViewController alloc]init];  
UINavigationController *Nav = [[UINavigationController alloc]initWithRootViewController:login];  
[self.navigationController presentModalViewController:Nav animated:YES]; 
           

轉載于:https://www.cnblogs.com/xiejw/p/5229484.html