玩转【斗鱼直播APP】系列之首页导航栏设置

首页导航栏设置

重点掌握内容

  • 导航栏添加Items
  • 给系统的类扩充方法
  • 扩充便利构造函数

效果展示

  • 在iOS开发中,导航栏的设置是必不可少的一部分。
  • 通常导航栏中会放很多的UIBarButtonItem,以方便用于操作对应的功能
  • 斗鱼首页导航条展示

直接实现(最low方式)

  • 不管是左侧logo的Item,还是右侧的三个Item都是有点击效果的。
  • 因此在实现时,最好创建UIButton,通过给UIButton设置图片来实现。并且让UIButton作为UIBarButtonItem的customView
  • 实现代码如下:
 
 
  1. // MARK:- 设置导航栏内容
  2. extension HomeViewController {
  3. // MARK: 设置导航栏
  4. private func setupNavigationBar() {
  5. setupNavigationLeftBar()
  6. setupNavigationRightBar()
  7. }
  8. private func setupNavigationLeftBar() {
  9. let btn = UIButton()
  10. btn.setImage(UIImage(named: "logo"), forState: .Normal)
  11. btn.sizeToFit()
  12. btn.addTarget(self, action: #selector(self.leftItemClick), forControlEvents: .TouchUpInside)
  13. navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
  14. }
  15. private func setupNavigationRightBar() {
  16. // 1.确定UIButton的尺寸
  17. let size = CGSize(width: 40, height: 44)
  18. // 2.创建历史的Item
  19. let historyBtn = UIButton()
  20. historyBtn.setImage(UIImage(named: "image_my_history"), forState: .Normal)
  21. historyBtn.setImage(UIImage(named: "image_my_history_click"), forState: .Highlighted)
  22. historyBtn.addTarget(self, action: #selector(self.historyItemClick), forControlEvents: .TouchUpInside)
  23. historyBtn.frame = CGRect(origin: CGPointZero, size: size)
  24. let historyItem = UIBarButtonItem(customView: historyBtn)
  25. // 3.创建搜索的Item
  26. let searchBtn = UIButton()
  27. searchBtn.setImage(UIImage(named: "btn_search"), forState: .Normal)
  28. searchBtn.setImage(UIImage(named: "btn_search_clicked"), forState: .Highlighted)
  29. searchBtn.addTarget(self, action: #selector(self.searchItemClick), forControlEvents: .TouchUpInside)
  30. searchBtn.frame = CGRect(origin: CGPointZero, size: size)
  31. let searchItem = UIBarButtonItem(customView: searchBtn)
  32. // 4.创建二维码的Item
  33. let qrcodeBtn = UIButton()
  34. qrcodeBtn.setImage(UIImage(named: "image_scan"), forState: .Normal)
  35. qrcodeBtn.setImage(UIImage(named: "image_scan_click"), forState: .Highlighted)
  36. qrcodeBtn.addTarget(self, action: #selector(self.qrCodeItemClick), forControlEvents: .TouchUpInside)
  37. qrcodeBtn.frame = CGRect(origin: CGPointZero, size: size)
  38. let qrcodeItem = UIBarButtonItem(customView: qrcodeBtn)
  39. navigationItem.rightBarButtonItems = [historyItem, searchItem , qrcodeItem]
  40. }
  41. // MARK: 导航栏的事件处理
  42. @objc private func leftItemClick() {
  43. print("点击了logo")
  44. }
  45. @objc private func qrCodeItemClick() {
  46. print("点击了二维码")
  47. }
  48. @objc private func searchItemClick() {
  49. print("点击了搜索")
  50. }
  51. @objc private func historyItemClick() {
  52. print("点击了历史")
  53. }
  54. }

优化上述代码(系统类扩充方法)

  • 上述方式虽然实现了功能,但是我们发现重复代码太多。
  • 并且如果其他地方也用到类似的功能,还是要写大量代码。
  • 最好的方式就是进行抽取?
  • 如何抽取呢?
    • 在OC中我们通常给系统的类抽取分类,在分类中给系统的类扩充方法
    • Swift也是类似,只是Swift使用extension,表示对系统的类进行扩充
  • 比如我们给系统的类抽取类方法,代码如下:
  • 给系统类扩充类方法
 
 
  1. extension UIBarButtonItem {
  2. class func createBarButtonItem(imageName : String, highImageName : String = "", size : CGSize = CGSizeZero, target : AnyObject? = nil, action : Selector = nil) -> UIBarButtonItem {
  3. // 1.创建UIButton
  4. let btn = UIButton(type: .Custom)
  5. // 2.给UIButton设置属性
  6. btn.setImage(UIImage(named: imageName), forState: .Normal)
  7. if highImageName != "" {
  8. btn.setImage(UIImage(named: highImageName), forState: .Highlighted)
  9. }
  10. // 3.设置尺寸
  11. if size == CGSizeZero {
  12. btn.sizeToFit()
  13. } else {
  14. btn.frame = CGRect(origin: CGPointZero, size: size)
  15. }
  16. // 4.监听点击
  17. btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
  18. return UIBarButtonItem(customView: btn)
  19. }
  20. }
  • 使用方式:
 
 
  1. // MARK: 设置导航栏
  2. private func setupNavigationBar() {
  3. setupNavigationLeftBar()
  4. setupNavigationRightBar()
  5. }
  6. private func setupNavigationLeftBar() {
  7. navigationItem.leftBarButtonItem = UIBarButtonItem.createBarButtonItem("logo", target: self, action: #selector(self.leftItemClick))
  8. }
  9. private func setupNavigationRightBar() {
  10. let size = CGSize(width: 40, height: 44)
  11. let historyItem = UIBarButtonItem.createBarButtonItem("image_my_history", highImageName: "image_my_history_click", size: size, target: self, action: #selector(self.historyItemClick))
  12. let searchItem = UIBarButtonItem.createBarButtonItem("btn_search", highImageName: "btn_search_click", size: size, target: self, action: #selector(self.searchItemClick))
  13. let qrCodeItem = UIBarButtonItem.createBarButtonItem("image_scan", highImageName: "image_scan_click", size: size, target: self, action: #selector(self.qrCodeItemClick))
  14. navigationItem.rightBarButtonItems = [historyItem, searchItem , qrCodeItem]
  15. }

扩充遍历构造函数(推荐做法)

  • 遍历构造函数特点
    • 构造函数前以convenience开头
    • 必须明确调用设计构造函数:例如self.init()
  • 构造函数写法:
 
 
  1. extension UIBarButtonItem {
  2. convenience init(imageName : String, highImageName : String = "", size : CGSize = CGSizeZero, target : AnyObject? = nil, action : Selector = nil) {
  3. let btn = UIButton(type: .Custom)
  4. btn.setImage(UIImage(named: imageName), forState: .Normal)
  5. if highImageName != "" {
  6. btn.setImage(UIImage(named: highImageName), forState: .Highlighted)
  7. }
  8. if size == CGSizeZero {
  9. btn.sizeToFit()
  10. } else {
  11. btn.frame = CGRect(origin: CGPointZero, size: size)
  12. }
  13. btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
  14. self.init(customView: btn)
  15. }
  16. }
  • 使用方式:
 
 
  1. // MARK: 设置导航栏
  2. private func setupNavigationBar() {
  3. setupNavigationLeftBar()
  4. setupNavigationRightBar()
  5. }
  6. private func setupNavigationLeftBar() {
  7. navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "logo", target: self, action: #selector(self.leftItemClick))
  8. }
  9. private func setupNavigationRightBar() {
  10. let size = CGSize(width: 40, height: 44)
  11. let historyItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "image_my_history_click", size: size, target: self, action: #selector(self.historyItemClick))
  12. let searchItem = UIBarButtonItem(imageName: "btn_search", highImageName: "btn_search_click", size: size, target: self, action: #selector(self.searchItemClick))
  13. let qrCodeItem = UIBarButtonItem(imageName: "image_scan", highImageName: "image_scan_click", size: size, target: self, action: #selector(self.qrCodeItemClick))
  14. navigationItem.rightBarButtonItems = [historyItem, searchItem , qrCodeItem]
  15. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值