PKCategoryView 0.1.3

PKCategoryView 0.1.3

Pramod Kumar维护。




PKCategoryView

CocoaPods codebeat badge

PKCategoryView 帮助您创建动态的 UITabBarController,它可以在滚动或点击标签时进行管理。

preview

系统要求

  • iOS 11.0+
  • Xcode 10.0+

安装

对于手动安装,将资源文件夹拖入您的项目。

或使用CocoaPod,在您的 Podfile 中添加以下行:

pod 'PKCategoryView'

用法

静态标签使用

它将调整 PKCategoryView 中所有标签的宽度。

preview

设置示例代码

//setting up the visual of the navBar
var config = PKCategoryViewConfiguration()
config.isNavBarScrollEnabled = false
config.normalColor = #colorLiteral(red: 0, green: 0.8, blue: 0.6, alpha: 1).withAlphaComponent(0.5)
config.selectedColor = #colorLiteral(red: 0, green: 0.8, blue: 0.6, alpha: 1)
config.indicatorColor = #colorLiteral(red: 0, green: 0.8, blue: 0.6, alpha: 1)

//frame for the category view
let rect = CGRect(x: 10.0, y: 60.0, width: (self.view.frame.size.width - 20.0), height: (self.view.frame.size.height - 70.0))

//setting up the tabs
let titleArr = ["Sushi", "Kebab", "Pizza", "Bread"]
let allTab = titleArr.map { PKCategoryItem(title: $0, normalImage: #imageLiteral(resourceName: "1"), selectedImage:#imageLiteral(resourceName: "2")) }

//create the child ViewControllers for the each tab
var allChild: [TabChildVC] = []
for (idx, ttl) in titleArr.enumerated() {
if let vc =  self.storyboard?.instantiateViewController(withIdentifier: "TabChildVC") as? TabChildVC {
vc.message = "Showing for \(ttl)"
vc.view.backgroundColor = (idx%2 == 0) ? UIColor.green.withAlphaComponent(0.3) : UIColor.yellow.withAlphaComponent(0.3)
allChild.append(vc)
}
}

//add category view to the viewController's desired view.
let catView = PKCategoryView(frame: rect, categories: allTab, childVCs: allChild, configuration: config, parentVC: self)
catView.delegate = self
self.view.addSubview(catView)

动态选项卡

这将使导航栏可滚动。

preview

设置示例代码

//setting up the visual of the navBar
var config = PKCategoryViewConfiguration()
config.isNavBarScrollEnabled = true
config.normalColor = #colorLiteral(red: 0, green: 0.8, blue: 0.6, alpha: 1).withAlphaComponent(0.5)
config.selectedColor = #colorLiteral(red: 0, green: 0.8, blue: 0.6, alpha: 1)
config.indicatorColor = #colorLiteral(red: 0, green: 0.8, blue: 0.6, alpha: 1)

//frame for the category view
let rect = CGRect(x: 10.0, y: 60.0, width: (self.view.frame.size.width - 20.0), height: (self.view.frame.size.height - 70.0))

//setting up the tabs
let titleArr = ["Pizza", "Sushi", "Bread", "Chocolate", "Massaman curry", "Buttered popcorn", "Hamburger", "Chicken", "Rendang", "Donuts"]
let allTab = titleArr.map { PKCategoryItem(title: $0, normalImage: #imageLiteral(resourceName: "1"), selectedImage:#imageLiteral(resourceName: "2")) }

//create the child ViewControllers for the each tab
var allChild: [TabChildVC] = []
for (idx, ttl) in titleArr.enumerated() {
if let vc =  self.storyboard?.instantiateViewController(withIdentifier: "TabChildVC") as? TabChildVC {
vc.message = "Showing for \(ttl)"
vc.view.backgroundColor = (idx%2 == 0) ? UIColor.green.withAlphaComponent(0.3) : UIColor.yellow.withAlphaComponent(0.3)
allChild.append(vc)
}
}

//add category view to the viewController's desired view.
let catView = PKCategoryView(frame: rect, categories: allTab, childVCs: allChild, configuration: config, parentVC: self)
catView.delegate = self
self.view.addSubview(catView)

添加徽章

要在任何索引处设置徽章计数,请调用类别视图的 setBadge 方法。设置示例代码:

catView.setBadge(count: 4, atIndex: 1)

注意:徽章可以是计数或仅一个点,请参考配置属性以设置相关内容。

preview

代理

只需将协议 PKCategoryViewDelegate 实现到 UIViewController 类中,确认协议后将有两种方法

  1. func categoryView(_ view: PKCategoryView, willSwitchIndexFrom fromIndex: Int, to toIndex: Int) 在切换标签前调用。
  2. func categoryView(_ view: PKCategoryView, didSwitchIndexTo toIndex: Int) 在标签切换后立即调用。

有用属性

对于 NavBarView 和 ContentView

有一个名为 PKCategoryViewConfiguration 的结构,用于提供类别视图的配置。一些有用的属性包括:

  1. navBarHeight 用于指定类别视图底部导航栏的高度。默认:44.0

  2. isNavBarScrollEnabled 用于决定导航栏是否可滚动。默认:false

  3. defaultFont 用于提供未选中标签的字体。默认:UIFont.systemFont(ofSize: 15.0)

  4. selectedFont 用于提供选中标签的字体。默认:UIFont.systemFont(ofSize: 17.0)

  5. normalColor用于为非选中标签提供颜色。默认值:#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)

  6. selectedColor用于为选中标签提供颜色。默认值:#colorLiteral(red: 0.9568627451, green: 0.6784313725, blue: 0.3843137255, alpha: 1)

  7. showBottomSeparator用于隐藏/显示导航栏和内容视图之间的分隔符。默认:true

  8. bottomSeparatorColor用于为底部分隔符提供颜色。默认:UIColor.lightGray

  9. showIndicator用于隐藏/显示选中标签下面的指示器。默认:true

  10. indicatorHeight用于为指示器视图提供高度。默认:2.0

  11. indicatorColor用于为指示器视图提供颜色。默认:#colorLiteral(red: 0.9568627451, green: 0.6784313725, blue: 0.3843137255, alpha: 1)

用于徽章
  1. badgeBackgroundColor用于为徽章视图提供背景颜色。默认:UIColor.red
  2. badgeTextColor用于为徽章视图提供文本颜色。默认:UIColor.white
  3. shouldShowBadgeCount用于决定显示为圆点还是显示徽章计数。默认:#colorLiteral(red: 0.9568627451, green: 0.6784313725, blue: 0.3843137255, alpha: 1)
在点状徽章情况下很有用
  1. badgeDotSize用于为徽章视图提供大小。默认:CGSize(width: 8.0, height: 8.0)
在计数徽章情况下很有用
  1. badgeTextFont用于为徽章视图提供文本字体。默认:UIFont.systemFont(ofSize: 12.0)
  2. badgeInset用于为徽章内容提供填充。默认:UIEdgeInsets(top: 1.0, left: 3.0, bottom: 1.0, right: 3.0)
  3. maxBadgeCount用于限制徽章计数。如果计数超过maxBadgeCount,将转换成+格式,如:99+ 默认:99
  4. badgeBorderWidth用于为徽章视图提供边框宽度。默认:0.0
  5. badgeBorderColor用于为徽章视图提供边框颜色。默认:UIColor.clear

许可

PKCategoryView在MIT许可证下发布。