iOS实现简单的抽屉式侧栏——MMDraweController的使用

新建项目

新建一个名叫MMDrawerControllerDemo的项目,language选择Swift,storyboard示意图如下,中间是一个Button push 到某一个页面,将起始页面的title改为Home。

新建一个文件,选择cocoa touch class,命名为SecondViewController

在storyboard中选择右边的ViewController,在identity inspector中将其class设置为SecondViewController,同时设置一个storyboard ID

导入MMDrawerController

从终端进入工程目录:
touch Podfile (创建Podfile文件,该文件用于描述第三方库信息)
vim Podfile (编辑Podfile文件,编辑第三方库信息)
Podfile编辑界面输入以下内容:
platform :ios
pod 'MMDrawerController', '~> 0.5.7'
完成后先按esc键退出编辑  —>  再输入 :wq ——>  回车,输入以下命令完成安装:
pod install --verbose --no-repo-update (安装第三方库)
安装完成后会出现如下提示,下次打开工程时使用workspace文件打开:
由于这里使用的是由OC编写的第三方库,因此还要使用一个桥接文件来连接导入的库和工程,新建一个MMDrawerControllerDemo-Bridge-Header.h文件:
 在工程的设置目录下填写桥接文件的完整路径:
 

创建侧栏

新建一个cocoa touch class文件,命名为LeftDrawerViewController,同时新建一个LeftDrawerTableViewCell。

其实可以同时在主页的左右两边各添加一个侧栏,但是我这里仅仅是在左侧添加。
接下来,在viewDidLoad()方法之前加上:

var tableView: UITableView!

在viewDidLoad()方法的最后加上:

tableView = UITableView(frame: CGRectZero, style: .Plain)
tableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height) tableView.delegate = self tableView.dataSource = self view.addSubview(tableView) tableView.registerNib(UINib(nibName: "LeftDrawerTableViewCell", bundle: nil), forCellReuseIdentifier: "LeftDrawerTableViewCell")

在class的外部加上以下代码:

// MARK: - table view delegate, table view datasource
extension LeftDrawerViewController: UITableViewDataSource, UITableViewDelegate { func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("LeftDrawerTableViewCell", forIndexPath: indexPath) as! LeftDrawerTableViewCell return cell } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } }

 

转载于:https://www.cnblogs.com/luoxiaoxi/p/5247747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值