github地址:https://github.com/SwimBoys/YYSegmentViewController
一、页面介绍
- YYSegmentConfig:配置文件
- YYSegmentViewController:控制器
- YYContainerScrollView:控制器view底部的ScrollView
- YYControllerPageView:控制器view的容器
- YYSegmentedView:上边title的底部view
- YYSegmentItemView:装title和指示器的view
- YYIndicatorView:指示器view
二、添加控制器方法
1. 直接添加控制器的view
let config = YYSegmentConfig()
let vc = YYSegmentViewController(config)
self.addChild(vc)
vc.view.frame = CGRect(x: 0, y: 130, width: view.bounds.size.width, height: 600)
self.view.addSubview(vc.view)
注意事项:一定要添加 self.addChild(vc),并且在初始化完成之后就得添加
2. 继承 YYSegmentViewController
image.png
注意事项:在 viewDidLoad() 方法中,在调用 super.viewDidLoad() 方法之前设置 config
三、使用技巧
1. 设置 Title
oneVc.tabBarItem.title = "第一章"
如果想改变 Title,可直接设置 tabBarItem.title
2. 设置角标
oneVc.tabBarItem.badgevalue = "112"
如果想改变角标,可直接设置 tabBarItem.badgevalue
3. 指示器
当设置 itemIndicatorViewShapeStyle 类型为椭圆和横杆时,itemIndicatorViewWidthChangeStyle不再起作用,默认为横杆
4. 页面刷新
当子view是普通view,想刷新整个页面时,最底层 YYContainerScrollView 是ScrollView,可添加刷新控件,当子view是 tableView 时,可设置刷新方式,整个刷新还是列表刷新
vc.containerScrView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(refreshControlAction))
当设置了headerView时,需要多一个设置,高度为 headerView 的高度
vc.containerScrView.mj_header.ignoredScrollViewContentInsetTop = 300
5. 自定义指示器
指示器view为 YYIndicatorView,可进行自定义,示例代码如下
vc.initDone = { [weak self] in
guard let this = self else {return}
let indicatorViewContentView = this.vc.segmentCtlView.indicatorView.contentView
let mixIndicatorView = MixIndicatorView(frame: indicatorViewContentView.bounds)
indicatorViewContentView.addSubview(mixIndicatorView)
mixIndicatorView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
}
config.itemIndicatorViewShapeStyle = .background(color: UIColor.clear, img: nil)
class MixIndicatorView: UIView {
let ellipseView = UIView()
let crossBarView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
ellipseView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.8)
ellipseView.frame = CGRect.init(x: 0, y: 0, width: bounds.width, height: 20)
ellipseView.center = CGPoint.init(x: bounds.width/2, y: bounds.height/2)
ellipseView.layer.cornerRadius = 10
ellipseView.autoresizingMask = [.flexibleWidth,.flexibleTopMargin,.flexibleBottomMargin]
addSubview(ellipseView)
let crossBarViewHight:CGFloat = 3
crossBarView.backgroundColor = UIColor.red
crossBarView.frame = CGRect.init(x: 0, y: bounds.height - crossBarViewHight, width: bounds.width, height: crossBarViewHight)
addSubview(crossBarView)
crossBarView.autoresizingMask = [.flexibleWidth,.flexibleTopMargin,.flexibleBottomMargin]
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
组合样式
三、使用示例
oneVc.tabBarItem.title = "第一章"
twoVc.tabBarItem.title = "第二章"
oneVc.tabBarItem.badgevalue = "112"
let headerV = UIImageView(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 300))
headerV.image = UIImage(named: "longzhu")
headerV.contentMode = .scaleAspectFill
headerV.backgroundColor = UIColor.red
let config = YYSegmentConfig()
config.containerControllerArr = [oneVc, twoVc]
let width = view.bounds.size.width / 2
config.segmentBackgroundColor = .yellow
config.headView = headerV
config.itemSpacing = 10
config.itemTitleSelectedScale = 1.5
config.itemWidthStyle = .equalToTitleWidth(margin: 0)
config.segmentControlHeight = 100
config.itemIndicatorViewBackgroundColor = UIColor.blue
config.itemBadgeStyle = .round
config.itemBadgeTitleColor = .blue
config.itemBadgevalueLabelOffset = CGPoint(x: 0, y: -5)
config.itemViewSegmentSelectedStyle = .gradient
config.itemBadgeSize = CGSize(width: 10, height: 10)
config.refreshType = .container
config.segmentControlPositionType = .top
config.itemIndicatorViewWidthChangeStyle = .stationary(baseWidth: width)
config.itemIndicatorViewShapeStyle = .crossBar(widthChangeStyle: .equalToItemWidth(margin: 0), height: 6)
config.isShowItemSeparatorLineView = true
config.itemSeparatorLineTopBottomMargin = (5, 5)
config.itemBadgeTitleFont = 9
let vc = YYSegmentViewController(config)
self.addChild(vc)
vc.view.frame = CGRect(x: 0, y: 130, width: view.bounds.size.width, height: 600)
self.view.addSubview(vc.view)
vc.containerScrView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(refreshControlAction))vc.containerScrView.mj_header.ignoredScrollViewContentInsetTop = 300
vc.segmentCtlView.clickAnimation = false
vc.pageView.isScrollEnabled = false
vc.segmentCtlView.delegate = self
vc.scrollViewDragTopOffsetYBlock = { (scr, offsetY) in
}
vc.initDone = {
}