欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解

 更新時(shí)間:2016年04月21日 09:41:47   作者:雙子座  
這篇文章主要介紹了iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法,文中講解了UIToolbar的相關(guān)編寫(xiě)以及使用xib方式創(chuàng)建可切換視圖程序的例子,需要的朋友可以參考下

關(guān)于UIToolbar
ToolBar工具欄是視圖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è)方法將其打開(kāi):

201642193049007.png (766×89)

在這里需要注意的是,與UINavigationBar類似,導(dǎo)航控制器擁有且只擁有一個(gè)UIToolBar實(shí)例,但UIToolBar擁有的UIBarButtonItem實(shí)例,是由視圖控制器進(jìn)行管理的,如下所示:

201642193128090.png (757×159)

工具欄風(fēng)格:

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

屬性:

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

方法:
※設(shè)置工具欄中的按鈕單元

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

※設(shè)置工具欄的背景圖像

復(fù)制代碼 代碼如下:

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

※獲取工具欄的背景圖像

復(fù)制代碼 代碼如下:

- (UIImage *)backgroundImageForToolbarPosition:(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)行Xcode,新建一個(gè)Empty Application,名稱為MultiView,其他設(shè)置如下圖:

201642193318520.png (517×221)

2、創(chuàng)建3個(gè)View Controller:
依次選擇File — New — New File,打開(kāi)如下窗口:

201642193334536.png (734×494)

找到UIViewController subclass并單擊Next,打開(kāi)下面的窗口:

201642193358623.png (594×321)

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

201642193415059.png (263×271)

3、為三個(gè)View Controller創(chuàng)建.xib文件:
依次選擇File — New — New File,打開(kāi)如下窗口:

201642193435862.png (733×494)

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

201642193455889.png (429×311)

輸入名稱RootView,單擊Create,創(chuàng)建了一個(gè).xib文件。用同樣的方法再創(chuàng)建兩個(gè).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;
}

復(fù)制代碼 代碼如下:
self.rootViewController = [[RootViewController alloc] initWithNibName:@"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:
單擊RootViewController.h,在其中添加兩個(gè)屬性和一個(gè)方法,如下:

#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、打開(kāi)RootView.xib,在坐邊選擇File's Owner,在右邊打開(kāi)Identity Inspector,在Class下拉菜單選擇RootViewController:

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

201642193620664.png (421×229)

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

201642193655682.png (269×202)

9、選擇File's Owner,按住Control鍵,拖到View,松開(kāi)鼠標(biāo),選擇view:

201642193709607.png (235×193)

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

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

在@implementation之后添加代碼:

@synthesize firstViewController;
@synthesize secondViewController;

接下來(lái)修改viewDidLoad方法,這個(gè)方法默認(rèn)是被注釋掉的,先去掉其周圍的注釋符,然后修改其代碼如下:

- (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、打開(kāi)FirstView.xib文件,選擇左邊的File's Owner,然后在Identity Inspector中選擇Class為FirstViewController;然后按住Control鍵從File's Owner圖標(biāo)拖到View,在彈出的菜單選擇view。為SecondView.xib進(jìn)行同樣的操作,不過(guò)Class選擇為SecondViewController。
12、打開(kāi)FirstView.xib文件,選擇View,打開(kāi)Attribute Inspector,進(jìn)行如下設(shè)置:

201642193814931.png (900×468)

對(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方法如下:

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

注意四個(gè)表示切換效果的常量:

UIViewAnimationTransitionFlipFromLeft
UIViewAnimationTransitionFlipFromRight
UIViewAnimationTransitionCurlDown
UIViewAnimationTransitionCurlUp

分別表示從左翻轉(zhuǎn)、從右翻轉(zhuǎn)、向下卷、向上卷。
運(yùn)行后翻頁(yè)效果如下:

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

相關(guān)文章

  • 解決ios模擬器不能彈出鍵盤問(wèn)題的方法

    解決ios模擬器不能彈出鍵盤問(wèn)題的方法

    這篇文章主要為大家詳細(xì)介紹了解決ios模擬器不能彈出鍵盤問(wèn)題的方法,大多數(shù)原因是誤用了快捷鍵,如何解決?感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS自定義UIButton點(diǎn)擊動(dòng)畫(huà)特效

    iOS自定義UIButton點(diǎn)擊動(dòng)畫(huà)特效

    這篇文章主要為大家詳細(xì)介紹了iOS自定義UIButton點(diǎn)擊動(dòng)畫(huà)特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iOS開(kāi)發(fā)教程之自定制圖片瀏覽器

    iOS開(kāi)發(fā)教程之自定制圖片瀏覽器

    最近發(fā)現(xiàn)許多常用的APP都有圖片瀏覽器,于是想仿照著自己寫(xiě)一個(gè),下面這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)教程之自定制圖片瀏覽器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-12-12
  • IOS中使用 CocoaAsyncSocket​

    IOS中使用 CocoaAsyncSocket​

    ios原生的socket用起來(lái)不是很直觀,所以我用的是CocoaAsyncSocket這個(gè)第三方庫(kù),對(duì)socket的封裝比較好,只是好像沒(méi)有帶外傳輸(out—of-band) 如果你的服務(wù)器需要發(fā)送帶外數(shù)據(jù),可能得想下別的辦法
    2016-02-02
  • IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器

    IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器

    這篇文章主要介紹了IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器的步驟,幫助大家更好的理解和學(xué)習(xí)使用ios開(kāi)發(fā),感興趣的朋友可以了解下
    2021-04-04
  • iOS自定義可展示、交互的scrollView滾動(dòng)條

    iOS自定義可展示、交互的scrollView滾動(dòng)條

    這篇文章主要為大家詳細(xì)介紹了iOS自定義可展示、交互的scrollView滾動(dòng)條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iOS中使用schema協(xié)議調(diào)用APP和使用iframe打開(kāi)APP的例子

    iOS中使用schema協(xié)議調(diào)用APP和使用iframe打開(kāi)APP的例子

    這篇文章主要介紹了iOS中使用schema協(xié)議調(diào)用APP和使用iframe打開(kāi)APP的例子,用在瀏覽器中打開(kāi)APP,需要的朋友可以參考下
    2014-08-08
  • Observing?KVO?Key-Value基本使用原理示例詳解

    Observing?KVO?Key-Value基本使用原理示例詳解

    這篇文章主要為大家介紹了Observing?KVO?Key-Value基本使用原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • iOS中長(zhǎng)條藍(lán)色按鈕(button)實(shí)現(xiàn)代碼

    iOS中長(zhǎng)條藍(lán)色按鈕(button)實(shí)現(xiàn)代碼

    本文通過(guò)實(shí)例代碼給大家介紹了iOS中長(zhǎng)條藍(lán)色按鈕(button)實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-08-08
  • iOS實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果

    iOS實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評(píng)論