IOS的模态窗口(modal)
生活随笔
收集整理的這篇文章主要介紹了
IOS的模态窗口(modal)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在iOS開發(fā)中,除了使用push方法切換控制器以外,modal也可以實現(xiàn)界面切換,使用modal方便快捷,任何控制器都可以使用modal展示出來,開發(fā)中在設(shè)置注冊,購物車,點贊等小功能的時候可以使用。
首先我們簡單了解下ViewController之間的跳轉(zhuǎn)
1、如果在 Storyboard中當前的 ViewController和要跳轉(zhuǎn)的ViewController之間的segue存在,則可以執(zhí)行performSegueWithIdentifier:sender:這個方法實現(xiàn)跳轉(zhuǎn)。
2、如果目標ViewController存在Storyboard中,但是沒有segue。你可以通過UIStoryboard的instantiateViewControllerWithIdentifier:這個方法獲取到它,然后再用你想要的方式實現(xiàn)跳轉(zhuǎn),如:壓棧
由于在iOS中并沒有專門的模態(tài)窗口類,模態(tài)窗口(modal)在iOS中只是視圖控制器顯示的一種方式,模態(tài)窗口不依賴于控制器容器(比如UITabBarController和UINavigationController),通常用于顯示獨立的內(nèi)容,在模態(tài)窗口顯示的時其他視圖的內(nèi)容無法進行操作,通俗的講,modal最常用的場景,新的場景完全蓋住了舊的那個。用戶無法再與上一個場景交互,除非他們先關(guān)閉這個場景。
modal的基本使用方法有如下兩種:
//使用modal彈出控制器 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion //關(guān)閉當初Modal出來的控制器 - (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;
系統(tǒng)自帶的modal動畫效果是從下往上的,但是我們可以通過modalTransitionStyle屬性,實現(xiàn)其他效果。
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
我們可以通過UIModalPresentationStyle屬性定義彈出風格
UIModalPresentationFullScreen充滿全屏,對于IOS7以后版本,如果彈出視圖控制器的wantsFullScreenLayout設(shè)置為YES的,則會填充到狀態(tài)欄下邊,否則不會填充到狀態(tài)欄之下。
UIModalPresentationPageSheet高度和當前屏幕高度相同,寬度和豎屏模式下屏幕寬度相同,剩余未覆蓋區(qū)域?qū)儼挡⒆柚褂脩酎c擊,這種彈出模式下,豎屏時跟 UIModalPresentationFullScreen的效果一樣,橫屏時候兩邊則會留下變暗的區(qū)域。
UIModalPresentationFormSheet高度和寬度均會小于屏幕尺寸,居中顯示,四周留下變暗區(qū)域。
UIModalPresentationCurrentContext這種模式下,彈出視圖控制器的彈出方式和它的父VC的方式相同。
同樣,我們也可以自定義跳轉(zhuǎn)動畫效果,代碼如下。
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風格的模塊窗口,如果有輸入框,整個視圖會根據(jù)鍵盤的顯隱自動調(diào)整。而且整個模塊窗口的大小是可以設(shè)置的(greenViewController.view.superview.bounds = CGRectFromString(framePortrait))。如果模塊窗口在顯示時自動定位光標到文本框,例如:- (void)viewDidAppear:(BOOL)animated {[contentTextView becomeFirstResponder];}
此時模塊視圖已經(jīng)根據(jù)鍵盤顯示調(diào)整到正確的位置,可能由于我們在之前設(shè)置模塊窗口的bounds
GreenViewController *greenViewController = [[GreenViewController alloc] init]; greenViewController .modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:greenViewController animated:YES]; greenViewController .view.superview.bounds = CGRectFromString(framePortrait);
這樣模塊窗口會在屏幕中間顯示,我們可以輸入相應(yīng)內(nèi)容。
在開發(fā)中會遇到這種情況,要先用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,將其跟控制器設(shè)置為registerViewConotroller,代碼如下:
LoginViewController *login = [[LoginViewController alloc]init]; UINavigationController *Nav = [[UINavigationController alloc]initWithRootViewController:login]; [self.navigationController presentModalViewController:Nav animated:YES];
,
總結(jié)
以上是生活随笔為你收集整理的IOS的模态窗口(modal)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 和初学者谈谈如何学好单片机编程技术
- 下一篇: [转]Aptana Studio 3配置