PKCategoryView
PKCategoryView 帮助您创建动态的 UITabBarController,它可以在滚动或点击标签时进行管理。
系统要求
- iOS 11.0+
- Xcode 10.0+
安装
对于手动安装,将资源文件夹拖入您的项目。
或使用CocoaPod,在您的 Podfile
中添加以下行:
pod 'PKCategoryView'
用法
静态标签使用
它将调整 PKCategoryView
中所有标签的宽度。
设置示例代码
//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)
动态选项卡
这将使导航栏可滚动。
设置示例代码
//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)
注意:徽章可以是计数或仅一个点,请参考配置属性以设置相关内容。
代理
只需将协议 PKCategoryViewDelegate
实现到 UIViewController
类中,确认协议后将有两种方法
func categoryView(_ view: PKCategoryView, willSwitchIndexFrom fromIndex: Int, to toIndex: Int)
在切换标签前调用。func categoryView(_ view: PKCategoryView, didSwitchIndexTo toIndex: Int)
在标签切换后立即调用。
有用属性
对于 NavBarView 和 ContentView
有一个名为 PKCategoryViewConfiguration
的结构,用于提供类别视图的配置。一些有用的属性包括:
-
navBarHeight
用于指定类别视图底部导航栏的高度。默认:44.0
-
isNavBarScrollEnabled
用于决定导航栏是否可滚动。默认:false
-
defaultFont
用于提供未选中标签的字体。默认:UIFont.systemFont(ofSize: 15.0)
-
selectedFont
用于提供选中标签的字体。默认:UIFont.systemFont(ofSize: 17.0)
-
normalColor
用于为非选中标签提供颜色。默认值:#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
-
selectedColor
用于为选中标签提供颜色。默认值:#colorLiteral(red: 0.9568627451, green: 0.6784313725, blue: 0.3843137255, alpha: 1)
-
showBottomSeparator
用于隐藏/显示导航栏和内容视图之间的分隔符。默认:true
-
bottomSeparatorColor
用于为底部分隔符提供颜色。默认:UIColor.lightGray
-
showIndicator
用于隐藏/显示选中标签下面的指示器。默认:true
-
indicatorHeight
用于为指示器视图提供高度。默认:2.0
-
indicatorColor
用于为指示器视图提供颜色。默认:#colorLiteral(red: 0.9568627451, green: 0.6784313725, blue: 0.3843137255, alpha: 1)
用于徽章
badgeBackgroundColor
用于为徽章视图提供背景颜色。默认:UIColor.red
badgeTextColor
用于为徽章视图提供文本颜色。默认:UIColor.white
shouldShowBadgeCount
用于决定显示为圆点还是显示徽章计数。默认:#colorLiteral(red: 0.9568627451, green: 0.6784313725, blue: 0.3843137255, alpha: 1)
在点状徽章情况下很有用
badgeDotSize
用于为徽章视图提供大小。默认:CGSize(width: 8.0, height: 8.0)
在计数徽章情况下很有用
badgeTextFont
用于为徽章视图提供文本字体。默认:UIFont.systemFont(ofSize: 12.0)
badgeInset
用于为徽章内容提供填充。默认:UIEdgeInsets(top: 1.0, left: 3.0, bottom: 1.0, right: 3.0)
maxBadgeCount
用于限制徽章计数。如果计数超过maxBadgeCount,将转换成+格式,如:99+
默认:99
badgeBorderWidth
用于为徽章视图提供边框宽度。默认:0.0
badgeBorderColor
用于为徽章视图提供边框颜色。默认:UIColor.clear
许可
PKCategoryView在MIT许可证下发布。