iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解_第1頁(yè)
iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解_第2頁(yè)
iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解_第3頁(yè)
iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解_第4頁(yè)
iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解這篇文章主要介紹了iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法,文中講解了UIToolbar的相關(guān)編寫(xiě)以及使用xib方式創(chuàng)建可切換視圖程序的例子,需要的朋友可以參考下關(guān)于UIToolbarToolBar工具欄是視圖View的屬性,可以在工具欄上添加工具欄按鈕Bar Button Item(可以是自定義的Custom、也可以是系統(tǒng)自帶的BarButtonSystemItem ),視圖控制器可以通過(guò)工具欄項(xiàng)對(duì)視圖中內(nèi)容進(jìn)行操作。注意事項(xiàng):在導(dǎo)航欄控制器中會(huì)有一個(gè)UIToolBar實(shí)例,但默認(rèn)是隱藏的,如果需要顯示,需要通過(guò)這個(gè)方法

2、將其打開(kāi):在這里需要注意的是,與UINavigationBar類似,導(dǎo)航控制器擁有且只擁有一個(gè)UIToolBar實(shí)例,但UIToolBar擁有的UIBarButtonItem實(shí)例,是由視圖控制器進(jìn)行管理的,如下所示:工具欄風(fēng)格:123456typedef NS_ENUM(NSInteger, UIBarStyle) UIBarStyleDefault = 0, /默認(rèn)風(fēng)格,藍(lán)色文字UIBarStyleBlack = 1, /黑色背景,褐色文字UIBarStyleBlackOpaque = 1, / 純黑色背景,白色文字UIBarStyleBlackTranslucent = 2, / 透明黑色

3、背景,白色文字;屬性:12345property(nonatomic) UIBarStyle barStyle; /工具欄風(fēng)格,默認(rèn)為藍(lán)色property(nonatomic,copy) NSArray *items; /工具欄中的按鈕單元,UIBarButtonItemproperty(nonatomic,assign,getter=isTranslucent) BOOL translucent /是否透明property(nonatomic,retain) UIColor *tintColor; /按鈕顏色property(nonatomic,retain) UIColor *barTin

4、tColor; /工具欄顏色方法:設(shè)置工具欄中的按鈕單元1- (void)setItems:(NSArray *)items animated:(BOOL)animated; 設(shè)置工具欄的背景圖像復(fù)制代碼 代碼如下:- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;獲取工具欄的背景圖像復(fù)制代碼 代碼如下:- (UIImage *)backgroundImageForToolb

5、arPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;設(shè)置工具欄的陰影圖像復(fù)制代碼 代碼如下:- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;獲取工具欄的陰影圖像復(fù)制代碼 代碼如下:- (UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom ;Tool Bar方式切換視圖1、創(chuàng)建工程:運(yùn)行

6、Xcode,新建一個(gè)Empty Application,名稱為MultiView,其他設(shè)置如下圖:2、創(chuàng)建3個(gè)View Controller:依次選擇File New New File,打開(kāi)如下窗口:找到UIViewController subclass并單擊Next,打開(kāi)下面的窗口:輸入名稱RootViewController,并且保證Subclass of選擇UIViewController,下面的兩個(gè)選框都不選;按照同樣的步驟新建兩個(gè)View Controller,名稱分別是FirstViewController和SecondViewController。建好后,在Project Nav

7、igation中顯示文件如下:3、為三個(gè)View Controller創(chuàng)建.xib文件:依次選擇File New New File,打開(kāi)如下窗口:在左邊選User Interface,右邊選View,單擊Next,在新窗口中的Device Family中選擇iPhone,單擊Next,打開(kāi)如下窗口:輸入名稱RootView,單擊Create,創(chuàng)建了一個(gè).xib文件。用同樣的方法再創(chuàng)建兩個(gè).xib,名稱分別是FirstView和SecondView。4、修改App Delegate:4.1 單擊AppDelegate.h,在其中添加代碼,在interface之前添加class RootViewC

8、ontroller;在end之前添加property (strong, nonatomic) RootViewController *rootViewController;添加之后的代碼如下:123456#import class RootViewController;interface AppDelegate : UIResponder property (strong, nonatomic) UIWindow *window;property (strong, nonatomic) RootViewController *rootViewController;end4.2 單擊AppDel

9、egate.m,修改其代碼。在implementation之前添加#import RootViewController.h,在implementation之后添加synthesize rootViewController;然后修改didFinishLaunchingWithOptions方法如下:1234567891011121314- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsself.window = UIWindow a

10、lloc initWithFrame:UIScreen mainScreen bounds;/ Override point for customization after application launch.self.rootViewController = RootViewController alloc initWithNibName:RootView bundle:nil; UIView *rootView = self.rootViewController.view; CGRect rootViewFrame = rootView.frame; rootViewFrame.orig

11、in.y += UIApplication sharedApplication.statusBarFrame.size.height; rootView.frame = rootViewFrame; self.window addSubview:rootView; self.window.backgroundColor = UIColor whiteColor;self.window makeKeyAndVisible;return YES; 復(fù)制代碼 代碼如下:self.rootViewController = RootViewController alloc initWithNibName

12、:RootView bundle:nil; 這行代碼用于從RootView.xib文件中初始化rootViewController,注意initWithNibName:RootView中不要后綴名.xib 復(fù)制代碼 代碼如下:rootViewFrame.origin.y += UIApplication sharedApplication.statusBarFrame.size.height; 使得RootViewController的視圖不會(huì)被狀態(tài)欄擋住5、修改RootViewController.h:?jiǎn)螕鬜ootViewController.h,在其中添加兩個(gè)屬性和一個(gè)方法,如下:1234

13、5678#import class FirstViewController;class SecondViewController;interface RootViewController : UIViewControllerproperty (strong, nonatomic) FirstViewController *firstViewController;property (strong, nonatomic) SecondViewController *secondViewController;- (IBAction)switchViews:(id)sender;end6、打開(kāi)Root

14、View.xib,在坐邊選擇Files Owner,在右邊打開(kāi)Identity Inspector,在Class下拉菜單選擇RootViewController:這樣,我們就可以從RootView.xib文件向RootViewController創(chuàng)建Outlet和Action了。7、為RootView.xib添加工具欄:打開(kāi)RootView.xib,拖一個(gè)Tool Bar到視圖上,雙擊Tool Bar上的按鈕,修改其名稱為Switch Views:8、添加Action映射:選中Switch Views按鈕,按住Control,拖到Files Owner,松開(kāi)鼠標(biāo)后選擇switchViews方法

15、:9、選擇Files Owner,按住Control鍵,拖到View,松開(kāi)鼠標(biāo),選擇view:10、修改RootViewController.m:打開(kāi)RootViewController.m文件,在implementation之前添加代碼:12#import FirstViewController.h#import SecondViewController.h在implementation之后添加代碼:12synthesize firstViewController;synthesize secondViewController;接下來(lái)修改viewDidLoad方法,這個(gè)方法默認(rèn)是被注釋掉的,

16、先去掉其周?chē)淖⑨尫?,然后修改其代碼如下:123456- (void)viewDidLoadself.firstViewController = FirstViewController alloc initWithNibName:FirstView bundle:nil;self.view insertSubview: firstViewController.view atIndex:0;super viewDidLoad;添加switchViews方法:12345678910111213141516- (IBAction)switchViews:(id)sender if (self.sec

17、ondViewController.view.superview = nil) if (self.secondViewController = nil) self.secondViewController = SecondViewController alloc initWithNibName:SecondView bundle:nil; firstViewController.view removeFromSuperview; self.view insertSubview:self.secondViewController.view atIndex:0; else if (self.fir

18、stViewController = nil) self.firstViewController = FirstViewController alloc initWithNibName:FirstView bundle:nil; secondViewController.view removeFromSuperview; self.view insertSubview:self.firstViewController.view atIndex:0; 修改didReceiveMemoryWarning方法:123456789- (void)didReceiveMemoryWarningsuper

19、 didReceiveMemoryWarning;if (self.firstViewCsuperview = nil) self.firstViewController = nil; else self.secondViewController = nil; 11、打開(kāi)FirstView.xib文件,選擇左邊的Files Owner,然后在Identity Inspector中選擇Class為FirstViewController;然后按住Control鍵從Files Owner圖標(biāo)拖到View,在彈出的菜單選擇view。為SecondView.xib進(jìn)行同樣的操作,不過(guò)Class選擇為Se

20、condViewController。12、打開(kāi)FirstView.xib文件,選擇View,打開(kāi)Attribute Inspector,進(jìn)行如下設(shè)置:對(duì)SecondView.xib進(jìn)行同樣設(shè)置,不過(guò)背景顏色設(shè)成紅色。13、此時(shí)運(yùn)行程序,你會(huì)看見(jiàn)剛啟動(dòng)的時(shí)候,程序顯示的綠色背景,輕觸Switch Views按鈕后,背景變成了紅色。不斷輕觸按鈕,背景不斷變換。14、添加切換背景的動(dòng)畫(huà)效果:打開(kāi)RootViewController.m,修改其中的switchViews方法如下:123456789101112131415161718192021- (IBAction)switchViews:(id)

21、sender UIView beginAnimations:View Flip context:nil; UIView setAnimationDuration:1.25; UIView setAnimationCurve:UIViewAnimationCurveEaseInOut;if (self.secondViewController.view.superview = nil) if (self.secondViewController = nil) self.secondViewController = SecondViewController alloc initWithNibName:SecondView bundle:nil; UIView setAnimationTransition: UIViewAnimationTransitionFlipFr

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論