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)
注意事項(xiàng):一定要添加 self.addChild(vc),并且在初始化完成之后就得添加
??? 2. 繼承 YYSegmentViewController
注意事項(xiàng):在 viewDidLoad() 方法中,在調(diào)用 super.viewDidLoad() 方法之前設(shè)置 config
三、使用技巧
??? 1. 設(shè)置 Title
oneVc.tabBarItem.title = "第一章"
如果想改變 Title杀迹,可直接設(shè)置 tabBarItem.title
??? 2. 設(shè)置角標(biāo)
oneVc.tabBarItem.badgeValue = "112"
如果想改變角標(biāo),可直接設(shè)置 tabBarItem.badgeValue
??? 3. 指示器
當(dāng)設(shè)置 itemIndicatorViewShapeStyle 類型為橢圓和橫桿時(shí),itemIndicatorViewWidthChangeStyle不再起作用崭别,默認(rèn)為橫桿
??? 4. 頁面刷新
當(dāng)子view是普通view,想刷新整個(gè)頁面時(shí)恐锣,最底層 YYContainerScrollView 是ScrollView茅主,可添加刷新控件,當(dāng)子view是 tableView 時(shí)土榴,可設(shè)置刷新方式诀姚,整個(gè)刷新還是列表刷新
vc.containerScrView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(refreshControlAction))
當(dāng)設(shè)置了headerView時(shí),需要多一個(gè)設(shè)置玷禽,高度為 headerView 的高度
vc.containerScrView.mj_header.ignoredScrollViewContentInsetTop = 300
??? 5. 自定義指示器
指示器view為 YYIndicatorView赫段,可進(jìn)行自定義,示例代碼如下
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 = {
}