A detailed approach to switching views using the toolbar toolbar in IOS applications _ios

Source: Internet
Author: User
Tags uikit

About Uitoolbar
The toolbar toolbar is the view's property, and you can add toolbar buttons bar button Item to the toolbar (either custom, custom, or system-led Barbuttonsystemitem). The view controller can manipulate the contents of the views from the toolbar items.

Precautions:
There is a Uitoolbar instance in the navigation bar controller, but the default is hidden, and if you need to display it, you need to open it in this way:

It should be noted here that, like Uinavigationbar, the navigation controller owns and has only one Uitoolbar instance, but the Uibarbuttonitem instance owned by Uitoolbar is managed by the view controller, as follows:

Toolbar style:

typedef ns_enum (Nsinteger, Uibarstyle) {
  Uibarstyledefault     = 0,    //default style, blue text
  uibarstyleblack      = 1 ,    //black background, brown text
  uibarstyleblackopaque   = 1,  //Pure black background, white text
  uibarstyleblacktranslucent = 2,  Transparent black background, white text
};

Property:

@property (nonatomic)    Uibarstyle Barstyle;  Toolbar style, default to Blue
@property (nonatomic,copy)  nsarray  *items;   button Unit in the toolbar, Uibarbuttonitem
@property (nonatomic,assign,getter=istranslucent) BOOL translucent//Transparent
@ Property (Nonatomic,retain) Uicolor *tintcolor;    Button Color
@property (nonatomic,retain) Uicolor *bartintcolor;//Toolbar color

Method:
※ Set the button cell in the toolbar

-(void) Setitems: (Nsarray *) Items animated: (BOOL) animated; 

※ Set the background image of the toolbar

Copy Code code as follows:

-(void) SetBackgroundImage: (UIImage *) backgroundimage fortoolbarposition: (uibarposition) Toporbottom barMetrics: ( Uibarmetrics) Barmetrics;

※ Get the background image of the toolbar

Copy Code code as follows:

-(UIImage *) Backgroundimagefortoolbarposition: (uibarposition) Toporbottom barmetrics: (uibarmetrics) barMetrics;


※ Set the shadow image of the toolbar

Copy Code code as follows:

-(void) Setshadowimage: (UIImage *) shadowimage fortoolbarposition: (uibarposition) Toporbottom;


※ Get the shadow image of the toolbar

Copy Code code as follows:

-(UIImage *) Shadowimagefortoolbarposition: (uibarposition) Toporbottom;

Tool Bar Mode Toggle View
1, create the project:
Run Xcode, create a new empty application, name MultiView, and other settings as shown below:

2, create 3 view Controller:
Select File-new-new File and open the following window:

Locate Uiviewcontroller subclass and click Next to open the following window:

Enter the name Rootviewcontroller and make sure that subclass of select Uiviewcontroller, the following two boxes are not selected; Follow the same steps to create a new two view Controller, The names are Firstviewcontroller and Secondviewcontroller respectively. When you are finished, the file appears in project navigation as follows:

3. Create a. xib file for three view controller:
Select File-new-new File and open the following window:

Select User Interface on the left, view on the right, click Next, choose the iphone in device family in the new window, click Next, and open the following window:

Enter a name Rootview, click Create to create a. xib file. Create two more xib in the same way, named Firstview and Secondview respectively.
4, modify the app Delegate:
4.1 Click AppDelegate.h, add the code, add the @class Rootviewcontroller before @interface, add @end before @property (strong, Nonatomic) Rootviewcontroller *rootviewcontroller; The following code is added:

#import <UIKit/UIKit.h>
@class rootviewcontroller;
@interface Appdelegate:uiresponder <UIApplicationDelegate>
@property (Strong, nonatomic) UIWindow *window;
@property (Strong, nonatomic) Rootviewcontroller *rootviewcontroller;
@end

4.2 Click APPDELEGATE.M to modify its code. Add #import "RootViewController.h" before @implementation, add @synthesize rootviewcontroller after @implementation; Then modify the Didfinishlaunchingwithoptions method as follows:

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

Copy Code code as follows:
Self.rootviewcontroller = [[Rootviewcontroller alloc] initwithnibname:@ "Rootview" bundle:nil];

This line of code is used to initialize the Rootviewcontroller from the Rootview.xib file, noting that initwithnibname:@ "Rootview" does not have a suffix name. xib
Ii
Copy Code code as follows:
ROOTVIEWFRAME.ORIGIN.Y + = [UIApplication sharedapplication].statusbarframe.size.height;

Makes the Rootviewcontroller view not blocked by the status bar
5, modify the RootViewController.h:
Click RootViewController.h to add two properties and one method, as follows:

#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, open rootview.xib, choose File ' Owner on the side, open identity Inspector on the right, select Rootviewcontroller in the Class Drop-down menu:

In this way, we can create outlet and action from the Rootview.xib file to Rootviewcontroller.
7. Add toolbars for Rootview.xib: Open Rootview.xib, drag a tool bar to the view, double-click the button on the tool bar to modify its name to switch views:

8. Add Action Mapping:
Select the Switch Views button, hold control, drag to file ' Owner, and then release the mouse to select the Switchviews method:

9, select file ' s Owner, hold down CONTROL key, drag to view, release mouse, select View:

10, modify the ROOTVIEWCONTROLLER.M:
Open the Rootviewcontroller.m file and add the code before @implementation:

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

Add code after @implementation:

@synthesize Firstviewcontroller;
@synthesize Secondviewcontroller;

Next, modify the Viewdidload method, which is annotated by default, removes the annotation around it, and then modifies its code as follows:

-(void) viewdidload
{
  Self.firstviewcontroller = [[Firstviewcontroller alloc] initwithnibname:@ "Firstview" Bundle:nil];
  [Self.view InsertSubview:firstViewController.view atindex:0];
  [Super Viewdidload];
}

Add Switchviews Method:

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

To modify the Didreceivememorywarning method:

-(void) didreceivememorywarning
{
  [super didreceivememorywarning];
  if (Self.firstViewController.view.superview = = nil) { 
    Self.firstviewcontroller = nil; 
  } else { 
    Self.secondviewcontroller = nil; 
  } 

11, open the Firstview.xib file, select the left of the file ' s Owner, and then select Class as Firstviewcontroller in the Identity inspector, and then hold down the control key from file ' s The owner icon is dragged to view and the view is selected in the pop-up menu. Do the same for secondview.xib, but class is chosen as Secondviewcontroller.
12, open Firstview.xib file, select View, open attribute Inspector, make the following settings:

Make the same settings for the secondview.xib, but the background color is set to red.
13, run the program at this time, you will see just started, the program shows the green background, touch the switch views button, the background turned red. Constantly touch the button, the background constantly changing.
14, add the animation effect of switching background:
Open rootviewcontroller.m and modify the Switchviews method as follows:

-(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.secondview 
    Controller = [[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] Initwi 
    thnibname:@ "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];
 }

Note Four constants that represent the switching effect:

Uiviewanimationtransitionflipfromleft
uiviewanimationtransitionflipfromright
Uiviewanimationtransitioncurldown
Uiviewanimationtransitioncurlup

Represents flip from left, flip from right, scroll down, volume up.
The paging effect after running is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.