This article records the use of the third-party library Wmpagecontroller controls in iOS development, wmpagecontroller similar to the Viewpager controls on the Android platform, primarily for pagination, and to toggle the page by gesture swipe. You can also toggle the page by clicking the title section, as shown in:
Wmpagecontroller's git address is: Https://github.com/wangmchn/WMPageController
Here's how to record the use of the Wmpagecontroller control:
(1) New Project Wmpagecontrollertest, and then through Cocoapods introduced Wmpagecontroller to the project, the contents of the Podfile file are as follows:
Platform:ios, ' 7.0 ' pod ' wmpagecontroller ', ' ~> 1.4.1 '
(2) Add the following code to the VIEWCONTROLLER.M file:
#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload { [ Super Viewdidload]; Additional setup after loading the view, typically from a nib. UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake ((a); Label.text = @ "Sports News"; [Self.view Addsubview:label];} -(void) didreceivememorywarning { [super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Here we just added a Uilabel tag to the Viewcontroller.
(3) Create another ViewController2 inherit from Uiviewcontroller, and add the following code to the VIEWCONTROLLER2.M file:
#import "ViewController2.h" @interface ViewController2 () @end @implementation viewcontroller2-(void) Viewdidload { [Super Viewdidload]; Do any additional setup after loading the view. UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake ((a); Label.text = @ "Entertainment News"; [Self.view Addsubview:label];} -(void) didreceivememorywarning { [super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
(4) Open the APPDELEGATE.M file, adding the following method to it:
#pragma mark Returns an Wmpagecontroller object-(Wmpagecontroller *) getpages {//wmpagecontroller contains an array of pages nsarray *controllers = [NSArray A Rraywithobjects:[viewcontroller class], [ViewController2 class], nil]; Wmpagecontroller control's title array Nsarray *titles = [Nsarray arraywithobjects:@ "Sports News", @ "Entertainment news", Nil]; Initialize the Wmpagecontroller object with the above two arrays wmpagecontroller *pagecontroller = [[Wmpagecontroller alloc] Initwithviewcontrollerclasses:controllers Andtheirtitles:titles]; Set Wmpagecontroller width of each caption pagecontroller.menuitemwidth = 100; Sets the height of the Wmpagecontroller title bar pagecontroller.menuheight = 35; Sets the color of Wmpagecontroller selected Caption pagecontroller.titlecolorselected = [Uicolor colorwithred:200 green:0 blue:0 alpha:1]; return pagecontroller;}
Then in APPDELEGATE.M-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (NSDictionary *) In the Launchoptions method, add the following code:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { Override point for customization after application launch. Wmpagecontroller *pagecontroller = [self getpages]; Uinavigationcontroller *navcontroller = [[Uinavigationcontroller alloc] initwithrootviewcontroller:pagecontroller]; Self.window.rootViewController = Navcontroller; return YES;}
The above code creates a Wmpagecontroller object, then initializes a navigation bar with it, and finally sets the window's root view controller to this navigation bar, and then we can run the program with the effect of:
The above is used in code Wmpagecontroller, according to the Demo,wmpagecontroller provided on GitHub can also be used in storyboard, The following shows how to use the Wmpagecontroller control in storyboard:
(1) Create a new project WMPageControllerTest2
(2) Open Main.storyboard, and then select Viewcontroller view to add a navigation bar to it, as shown in:
(3) The following creates a new Viewcontroller, named Newsviewcontroller, Also in Main.storyboard, drag a Viewcontroller view, set the view to Newsviewcontroller, and set the Storyboardid to "Viewcontroller" as shown in:
Add a Uilabel tag to the newly created Viewcontroller view, as shown in.
(4) Open the ViewController.h file and let Viewcontroller inherit from Wmpagecontroller, as shown in the code below:
#import <UIKit/UIKit.h> #import "WMPageController.h" @interface viewcontroller:wmpagecontroller#pragma Mark Header array @property (strong, nonatomic) Nsarray *titles; @end
(5) Edit the viewcontroller.m file with the following code:
#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller#pragma Mark initialization code-( Instancetype) Initwithcoder: (Nscoder *) Adecoder {self = [super Initwithcoder:adecoder]; if (self) {self.menuheight = 35; Self.menuitemwidth = 100; Self.menuviewstyle = Wmmenuviewstyleline; Self.titles = [Nsarray arraywithobjects:@ "Entertainment News", @ "Sports News", Nil]; } return self;} -(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view, typically from a nib.} #pragma mark returns the title of index-(NSString *) Pagecontroller: (Wmpagecontroller *) Pagecontroller Titleatindex: (Nsinteger) index {return [self.titles Objectatindex:index];} #pragma mark returns the number of child pages-(Nsinteger) Numbersofchildcontrollersinpagecontroller: (Wmpagecontroller *) Pagecontroller { return [Self.titles count];} #pragma mark returns a page of index corresponding to the page from storyboard-(Uiviewcontroller *) Pagecontroller: (Wmpagecontroller *) Pagecontroller Viewcontrolleratindex: (Nsinteger) INdex {Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" bundle:nil]; Uiviewcontroller *controller = [Storyboard instantiateviewcontrollerwithidentifier:@ "Viewcontroller"]; The identifer here is the Storyboardid return controller we set previously;} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @endHere you can run the program, the effect is as follows:
Note that there are several styles to choose from in the title section of Wmpagecontroller, and the enumeration values for the styles are as follows:
typedef ns_enum (Nsuinteger, Wmmenuviewstyle) { wmmenuviewstyledefault, //default wmmenuviewstyleline, Underlined (to select the same font size, set check and unchecked size) wmmenuviewstylefoold, //Influx effect (padding) wmmenuviewstylefooldhollow,// Influx effect (hollow)};
The use of iOS development Wmpagecontroller