iOS開發之WMPageController的使用

來源:互聯網
上載者:User

iOS開發之WMPageController的使用

這一篇記錄的是iOS開發中第三方庫WMPageController控制項的使用方法,WMPageController類似於Android平台上的ViewPager控制項,主要是用來分頁顯示內容的,可以通過手勢滑動來切換頁面,也可以通過點擊標題部分來切換頁面,如所示:

WMPageController的git地址為:https://github.com/wangmchn/WMPageController

下面就記錄WMPageController控制項的使用方法:

(1)建立工程WMPageControllerTest,然後通過cocoapods引入WMPageController到項目中,Podfile檔案的內容如下:

 

platform:ios, '7.0'pod 'WMPageController', '~> 1.4.1'
(2)在ViewController.m檔案中加入以下代碼:

 

 

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];    label.text = @"體育新聞";    [self.view addSubview:label];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
這裡我們僅僅只是在ViewController中加入了一個UILabel標籤

 

(3)再建立一個ViewController2繼承自UIViewController,並且在ViewController2.m檔案中加入以下代碼:

 

#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(50, 50, 100, 50)];    label.text = @"娛樂新聞";    [self.view addSubview:label];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
(4)開啟AppDelegate.m檔案,在其中加入下面一個方法:

 

 

#pragma mark 返回一個WMPageController對象- (WMPageController *) getPages {    //WMPageController中包含的頁面數組    NSArray *controllers = [NSArray arrayWithObjects:[ViewController class], [ViewController2 class], nil];    //WMPageController控制項的標題數組    NSArray *titles = [NSArray arrayWithObjects:@"體育新聞", @"娛樂新聞", nil];    //用上面兩個數組初始化WMPageController對象    WMPageController *pageController = [[WMPageController alloc] initWithViewControllerClasses:controllers andTheirTitles:titles];    //設定WMPageController每個標題的寬度    pageController.menuItemWidth = 100;    //設定WMPageController標題列的高度    pageController.menuHeight = 35;    //設定WMPageController選中的標題的顏色    pageController.titleColorSelected = [UIColor colorWithRed:200 green:0 blue:0 alpha:1];    return pageController;}
然後在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中,加入如下代碼:

 

 

- (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;}
上面的代碼建立了一個WMPageController對象,然後用它初始化了一個導覽列,最後將視窗的根視圖控制器設定為這個導覽列,然後我們就可以運行程式了,效果如:

 


上面是在代碼中使用WMPageController,根據github上提供的demo,WMPageController還可以在Storyboard中使用,下面說明如何在storyboard中使用WMPageController控制項:

(1)建立一個項目WMPageControllerTest2

(2)開啟Main.storyboard,然後選中ViewController視圖,給它添加一個導覽列,如所示:


(3)下面建立一個新的ViewController,取名為NewsViewController,並且在Main.storyboard中也拖入一個ViewController視圖,將該視圖設定為NewsViewController,然後將StoryboardID設定為“viewController”,如所示:

在新建立的ViewController視圖中加入一個UILabel標籤,如所示。

(4)開啟ViewController.h檔案,讓ViewController繼承自WMPageController,代碼如下所示:

 

#import #import "WMPageController.h"@interface ViewController : WMPageController#pragma mark 標題數組@property (strong, nonatomic) NSArray *titles;@end
(5)編輯ViewController.m檔案,代碼如下:

 

 

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController#pragma mark 初始化代碼- (instancetype)initWithCoder:(NSCoder *)aDecoder {    self = [super initWithCoder:aDecoder];    if(self) {        self.menuHeight = 35;        self.menuItemWidth = 100;        self.menuViewStyle = WMMenuViewStyleLine;        self.titles = [NSArray arrayWithObjects:@"娛樂新聞", @"體育新聞", nil];    }    return self;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}#pragma mark 返回index對應的標題- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {    return [self.titles objectAtIndex:index];}#pragma mark 返回子頁面的個數- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {    return [self.titles count];}#pragma mark 返回某個index對應的頁面,該頁面從Storyboard中擷取- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"viewController"]; //這裡的identifer是我們之前設定的StoryboardID    return controller;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
到這裡就可以運行程式了,效果如:

 

需要注意的是,WMPageController的標題部分有幾個樣式可以選擇,樣式的枚舉值如下:

 

typedef NS_ENUM(NSUInteger, WMMenuViewStyle) {    WMMenuViewStyleDefault,     // 預設    WMMenuViewStyleLine,        // 帶底線 (若要選中字型大小不變,設定選中和非選中大小一樣即可)    WMMenuViewStyleFoold,       // 湧入效果 (填充)    WMMenuViewStyleFooldHollow, // 湧入效果 (空心的)};





 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.