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]; 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


免責聲明!

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



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