亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

iOS應用中使用Toolbar工具欄方式切換視圖的方法詳解

2020-07-26 03:22:06
字體:
來源:轉載
供稿:網友

關于UIToolbar
ToolBar工具欄是視圖View的屬性,可以在工具欄上添加工具欄按鈕Bar Button Item(可以是自定義的Custom、也可以是系統自帶的BarButtonSystemItem ),視圖控制器可以通過工具欄項對視圖中內容進行操作。

注意事項:
在導航欄控制器中會有一個UIToolBar實例,但默認是隱藏的,如果需要顯示,需要通過這個方法將其打開:

201642193049007.png (766×89)

在這里需要注意的是,與UINavigationBar類似,導航控制器擁有且只擁有一個UIToolBar實例,但UIToolBar擁有的UIBarButtonItem實例,是由視圖控制器進行管理的,如下所示:

201642193128090.png (757×159)

工具欄風格:

typedef NS_ENUM(NSInteger, UIBarStyle) {  UIBarStyleDefault     = 0,    //默認風格,藍色文字  UIBarStyleBlack      = 1,    //黑色背景,褐色文字  UIBarStyleBlackOpaque   = 1,  // 純黑色背景,白色文字  UIBarStyleBlackTranslucent = 2,  // 透明黑色背景,白色文字};

屬性:

@property(nonatomic)    UIBarStyle barStyle;  //工具欄風格,默認為藍色@property(nonatomic,copy)  NSArray  *items;   //工具欄中的按鈕單元,UIBarButtonItem@property(nonatomic,assign,getter=isTranslucent) BOOL translucent //是否透明@property(nonatomic,retain) UIColor *tintColor;    //按鈕顏色@property(nonatomic,retain) UIColor *barTintColor; //工具欄顏色

方法:
※設置工具欄中的按鈕單元

- (void)setItems:(NSArray *)items animated:(BOOL)animated; 

※設置工具欄的背景圖像

復制代碼 代碼如下:

- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;

※獲取工具欄的背景圖像

復制代碼 代碼如下:

- (UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;


※設置工具欄的陰影圖像

復制代碼 代碼如下:

- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;


 ※獲取工具欄的陰影圖像

復制代碼 代碼如下:

- (UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom ;

Tool Bar方式切換視圖
1、創建工程:
運行Xcode,新建一個Empty Application,名稱為MultiView,其他設置如下圖:

201642193318520.png (517×221)

2、創建3個View Controller:
依次選擇File ― New ― New File,打開如下窗口:

201642193334536.png (734×494)

找到UIViewController subclass并單擊Next,打開下面的窗口:

201642193358623.png (594×321)

輸入名稱RootViewController,并且保證Subclass of選擇UIViewController,下面的兩個選框都不選;按照同樣的步驟新建兩個View Controller,名稱分別是FirstViewController和SecondViewController。建好后,在Project Navigation中顯示文件如下:

201642193415059.png (263×271)

3、為三個View Controller創建.xib文件:
依次選擇File ― New ― New File,打開如下窗口:

201642193435862.png (733×494)

在左邊選User Interface,右邊選View,單擊Next,在新窗口中的Device Family中選擇iPhone,單擊Next,打開如下窗口:

201642193455889.png (429×311)

輸入名稱RootView,單擊Create,創建了一個.xib文件。用同樣的方法再創建兩個.xib,名稱分別是FirstView和SecondView。
4、修改App Delegate:
4.1 單擊AppDelegate.h,在其中添加代碼,在@interface之前添加@class RootViewController;在@end之前添加@property (strong, nonatomic) RootViewController *rootViewController;添加之后的代碼如下:

#import <UIKit/UIKit.h>@class RootViewController;@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) RootViewController *rootViewController;@end

4.2 單擊AppDelegate.m,修改其代碼。在@implementation之前添加#import "RootViewController.h",在@implementation之后添加@synthesize rootViewController;然后修改didFinishLaunchingWithOptions方法如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{  self.window = [[UIWindow alloc] 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.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;   rootView.frame = rootViewFrame;   [self.window addSubview:rootView];   self.window.backgroundColor = [UIColor whiteColor];  [self.window makeKeyAndVisible];  return YES;}

復制代碼 代碼如下:
self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil];

這行代碼用于從RootView.xib文件中初始化rootViewController,注意initWithNibName:@"RootView"中不要后綴名.xib
② 
復制代碼 代碼如下:
rootViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;

使得RootViewController的視圖不會被狀態欄擋住
5、修改RootViewController.h:
單擊RootViewController.h,在其中添加兩個屬性和一個方法,如下:

#import <UIKit/UIKit.h>@class FirstViewController;@class SecondViewController;@interface RootViewController : UIViewController@property (strong, nonatomic) FirstViewController *firstViewController;@property (strong, nonatomic) SecondViewController *secondViewController;- (IBAction)switchViews:(id)sender;@end

6、打開RootView.xib,在坐邊選擇File's Owner,在右邊打開Identity Inspector,在Class下拉菜單選擇RootViewController:

這樣,我們201642193602292.png (714×197)就可以從RootView.xib文件向RootViewController創建Outlet和Action了。
7、為RootView.xib添加工具欄:打開RootView.xib,拖一個Tool Bar到視圖上,雙擊Tool Bar上的按鈕,修改其名稱為Switch Views:

201642193620664.png (421×229)

8、添加Action映射:
選中Switch Views按鈕,按住Control,拖到File's Owner,松開鼠標后選擇switchViews方法:

201642193655682.png (269×202)

9、選擇File's Owner,按住Control鍵,拖到View,松開鼠標,選擇view:

201642193709607.png (235×193)

10、修改RootViewController.m:
打開RootViewController.m文件,在@implementation之前添加代碼:

#import "FirstViewController.h"#import "SecondViewController.h"

在@implementation之后添加代碼:

@synthesize firstViewController;@synthesize secondViewController;

接下來修改viewDidLoad方法,這個方法默認是被注釋掉的,先去掉其周圍的注釋符,然后修改其代碼如下:

- (void)viewDidLoad{  self.firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];  [self.view insertSubview: firstViewController.view atIndex:0];  [super viewDidLoad];}

添加switchViews方法:

- (IBAction)switchViews:(id)sender {  if (self.secondViewController.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.firstViewController == nil) {       self.firstViewController =       [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];     }     [secondViewController.view removeFromSuperview];     [self.view insertSubview:self.firstViewController.view atIndex:0];   } }

修改didReceiveMemoryWarning方法:

- (void)didReceiveMemoryWarning{  [super didReceiveMemoryWarning];  if (self.firstViewController.view.superview == nil) {     self.firstViewController = nil;   } else {     self.secondViewController = nil;   } }

11、打開FirstView.xib文件,選擇左邊的File's Owner,然后在Identity Inspector中選擇Class為FirstViewController;然后按住Control鍵從File's Owner圖標拖到View,在彈出的菜單選擇view。為SecondView.xib進行同樣的操作,不過Class選擇為SecondViewController。
12、打開FirstView.xib文件,選擇View,打開Attribute Inspector,進行如下設置:

201642193814931.png (900×468)

對SecondView.xib進行同樣設置,不過背景顏色設成紅色。
13、此時運行程序,你會看見剛啟動的時候,程序顯示的綠色背景,輕觸Switch Views按鈕后,背景變成了紅色。不斷輕觸按鈕,背景不斷變換。
14、添加切換背景的動畫效果:
打開RootViewController.m,修改其中的switchViews方法如下:

- (IBAction)switchViews:(id)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: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];     [self.firstViewController.view removeFromSuperview];     [self.view insertSubview:self.secondViewController.view atIndex:0];   } else {     if (self.firstViewController == nil) {       self.firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];     }     [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES];     [self.secondViewController.view removeFromSuperview];     [self.view insertSubview:self.firstViewController.view atIndex:0];   }   [UIView commitAnimations]; }

注意四個表示切換效果的常量:

UIViewAnimationTransitionFlipFromLeftUIViewAnimationTransitionFlipFromRightUIViewAnimationTransitionCurlDownUIViewAnimationTransitionCurlUp

分別表示從左翻轉、從右翻轉、向下卷、向上卷。
運行后翻頁效果如下:

201642193904648.png (350×500)201642193920985.png (350×500)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久影院免费观看| 91福利视频在线观看| 亚洲免费影视第一页| 精品偷拍一区二区三区在线看| 九九热这里只有在线精品视| 中文字幕欧美精品日韩中文字幕| 日韩在线播放视频| 欧美日韩美女在线| 亚洲天堂色网站| 国产精品高清免费在线观看| 欧美性受xxxx白人性爽| 久久久视频在线| 国产精品久久久久久久久久东京| 中文字幕亚洲欧美日韩高清| 中文字幕在线观看日韩| 国产成人综合精品在线| 久久久久久久久久久91| 国产精品一区二区久久精品| 日韩亚洲在线观看| 欧美日韩精品在线观看| 国产人妖伪娘一区91| 国产欧美一区二区三区视频| 中文字幕日本欧美| 欧美美女操人视频| 久久精品99久久香蕉国产色戒| 欧美极度另类性三渗透| 高清一区二区三区日本久| 久久天天躁狠狠躁夜夜躁2014| 激情av一区二区| 亚洲一区二区自拍| 日韩免费高清在线观看| 粗暴蹂躏中文一区二区三区| 国产中文欧美精品| 国产激情久久久久| 亚洲欧洲午夜一线一品| 伊人久久大香线蕉av一区二区| 日韩在线视频免费观看| 精品伊人久久97| 日韩精品在线免费播放| 91精品视频在线免费观看| 欧美中文字幕在线视频| 91精品国产沙发| 伊人久久久久久久久久久| 亚洲第一精品夜夜躁人人躁| 国产精品7m视频| 777国产偷窥盗摄精品视频| 国产亚洲精品一区二区| 在线中文字幕日韩| 久久久亚洲欧洲日产国码aⅴ| 久久99精品国产99久久6尤物| 日韩一区二区福利| 国内外成人免费激情在线视频网站| 亚洲人精选亚洲人成在线| 亚洲图片在线综合| 日韩在线一区二区三区免费视频| 亚洲偷欧美偷国内偷| 国产精品成人国产乱一区| 国产亚洲成精品久久| 欧美日韩亚洲精品内裤| 久久精品国产v日韩v亚洲| 最近2019年好看中文字幕视频| 精品国产乱码久久久久久婷婷| 亚洲日韩中文字幕在线播放| 国产欧美在线播放| 成人写真视频福利网| 欧美—级高清免费播放| 精品国产一区二区三区久久久狼| 国产精品自产拍在线观| 欧美一区第一页| 欧美成人高清视频| 91美女片黄在线观看游戏| 精品久久久久久亚洲国产300| 一区二区三区美女xx视频| 国产高清视频一区三区| 欧美激情在线播放| 欧美噜噜久久久xxx| 亚洲第一精品夜夜躁人人爽| 久久国产精品久久久久久| 日韩在线视频免费观看高清中文| 国产精品观看在线亚洲人成网| 亚洲精品福利在线| 久久国产精品久久精品| 久久久亚洲天堂| 欧美成人精品在线| 国产精品入口日韩视频大尺度| 日韩中文字幕免费| 国产精品视频久| 亚洲九九九在线观看| 亚洲奶大毛多的老太婆| 亚洲新声在线观看| 欧美一区二区三区四区在线| 欧美性极品xxxx做受| 欧美乱人伦中文字幕在线| 欧美在线欧美在线| 菠萝蜜影院一区二区免费| 精品久久久久久久久久久| 黑人与娇小精品av专区| 欧美一区深夜视频| 亚洲精品久久久久久久久久久久久| 日韩欧美国产一区二区| 亚洲xxxx在线| 欧美精品日韩三级| 亚洲欧美综合另类中字| 黄网站色欧美视频| 黄色91在线观看| 国产一区二区视频在线观看| 日韩美女主播视频| 久久久久久久久久国产精品| 一本大道香蕉久在线播放29| 中文字幕日韩高清| 久久亚洲精品国产亚洲老地址| 日韩av一区在线观看| 国产日韩精品在线播放| 欧美一级黑人aaaaaaa做受| 色综合伊人色综合网站| 亚洲欧美成人网| 国产女同一区二区| 91精品国产高清| 久久99久国产精品黄毛片入口| 久久视频中文字幕| 一区二区三区高清国产| 亚洲午夜久久久久久久| 久久精品视频亚洲| 国产日韩精品电影| 国产精品视频大全| 91免费欧美精品| 欧美激情精品久久久久久蜜臀| 91香蕉嫩草神马影院在线观看| 超碰91人人草人人干| 97在线精品视频| 青草青草久热精品视频在线网站| 亚洲综合色激情五月| 国产精品一区二区三区免费视频| 日韩av网址在线| 欧美激情亚洲一区| 日韩a**中文字幕| 久久青草精品视频免费观看| 久久久久久久久久国产精品| 欧美精品在线免费| 欧美在线精品免播放器视频| 精品久久久久久亚洲精品| 亚洲伊人一本大道中文字幕| 夜色77av精品影院| 久久久精品国产亚洲| 一区二区亚洲精品国产| 91亚洲国产成人精品性色| 国产精品久久二区| 亚洲人成在线观看网站高清| 欧美二区在线播放| 国产69精品久久久久9| 欧美情侣性视频| 97人人爽人人喊人人模波多| 日韩精品极品毛片系列视频| 欧美激情欧美狂野欧美精品| 欧美亚洲一级片| 亚洲第一级黄色片| 亚洲欧美国产另类| 97精品久久久| 久久久久久中文| 欧美成年人网站| 亚洲一区二区中文字幕| 亚洲视频axxx| 国产亚洲精品久久久久动| 日韩大片在线观看视频|