Github鏈接:(https://github.com/MichaelHuyp/YPSegmentController)
self.segmentController.segmentBar.scrollMode = YPSegmentBarScrollModeNormal

使用說明書
#import "YPViewController.h"
#import "YPSegmentController.h"
#import "UIView+YPSegment.h"
#define YPColor_RGB(r, g, b) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:1.0]
#define YPRandomColor_RGB YPColor_RGB(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
@interface YPViewController ()
@property (nonatomic, weak) YPSegmentController *segmentController;
@end
@implementation YPViewController
#pragma mark - Lazy
- (YPSegmentController *)segmentController
{
if (!_segmentController) {
YPSegmentController *vc = [YPSegmentController new];
[self addChildViewController:vc];
_segmentController = vc;
}
return _segmentController;
}
#pragma mark - Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"2016互聯網十強企業";
self.automaticallyAdjustsScrollViewInsets = NO;
self.segmentController.view.frame = self.view.bounds;
[self.view addSubview:self.segmentController.view];
// 更新選項條的各項配置
[self.segmentController.segmentBar updateWithConfig:^(YPSegmentBarConfig *config) {
config.itemTitleNormalColor = [UIColor blueColor];
config.itemTitleSelectColor = [UIColor redColor];
}];
// 是否開啟漸變色
self.segmentController.segmentBar.enableTitleGradient = YES;
// 聯動模式是否開啟進度實時更新
self.segmentController.segmentBar.linkMode = YPSegmentBarLinkModeProgress;
// 選項條的滾動模式是否為居中模式
self.segmentController.segmentBar.scrollMode = YPSegmentBarScrollModeCenter;
UIViewController *vc1 = [UIViewController new];
vc1.title = @"騰訊";
vc1.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc2 = [UIViewController new];
vc2.title = @"阿里巴巴";
vc2.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc3 = [UIViewController new];
vc3.title = @"螞蟻金服";
vc3.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc4 = [UIViewController new];
vc4.title = @"百度";
vc4.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc5 = [UIViewController new];
vc5.title = @"京東";
vc5.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc6 = [UIViewController new];
vc6.title = @"網易";
vc6.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc7 = [UIViewController new];
vc7.title = @"小米科技";
vc7.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc8 = [UIViewController new];
vc8.title = @"滴滴出行";
vc8.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc9 = [UIViewController new];
vc9.title = @"陸金所";
vc9.view.backgroundColor = YPRandomColor_RGB;
UIViewController *vc10 = [UIViewController new];
vc10.title = @"美團-大眾點評";
vc10.view.backgroundColor = YPRandomColor_RGB;
// 配置子控制器
[self.segmentController setUpWithItems:@[vc1,vc2,vc3,vc4,vc5,vc6,vc7,vc8,vc9,vc10]];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self.segmentController updateWithConfig:^(YPSegmentControllerConfig *config) {
config.segmentBarTop = self.navigationController.navigationBar.bottom;
}];
}
YPSegmentBar.h
typedef NS_ENUM(NSInteger, YPSegmentBarScrollMode) {
YPSegmentBarScrollModeNormal, // 默認滾動模式
YPSegmentBarScrollModeCenter // 中間滾動模式
};
typedef NS_ENUM(NSInteger, YPSegmentBarLinkMode) {
YPSegmentBarLinkModeNormal, // 默認聯動模式
YPSegmentBarLinkModeProgress // 根據進度的聯動模式
};
@protocol YPSegmentBarDelegate;
@interface YPSegmentBar : UIView
/** 數據源 */
@property (nonatomic, copy) NSArray <NSString *> *items;
/** 代理 */
@property (nonatomic, weak) id <YPSegmentBarDelegate> delegate;
/** 當前選中的索引, 雙向設置 */
@property (nonatomic, assign) NSInteger selectIndex;
/** 滾動模式 */
@property (nonatomic, assign) YPSegmentBarScrollMode scrollMode;
/** 聯動模式 */
@property (nonatomic, assign) YPSegmentBarLinkMode linkMode;
/** 指示器進度 */
@property (nonatomic, assign) CGFloat indicatorProgress;
/** 是否開啟標題顏色漸變 默認為No */
@property (nonatomic, assign) BOOL enableTitleGradient;
/**
* 配置Bar的各種參數
*/
- (void)updateWithConfig:(void(^)(YPSegmentBarConfig *config))block;
@end
@protocol YPSegmentBarDelegate <NSObject>
- (void)segmentBar:(YPSegmentBar *)segmentBar didSelectedIndex:(NSInteger)toIndex fromIndex:(NSInteger)fromIndex;
@end
YPSegmentController.h
@interface YPSegmentController : UIViewController
/** 選項條 */
@property (nonatomic, weak) YPSegmentBar *segmentBar;
/**
* 設置數據源
*/
- (void)setUpWithItems:(NSArray <UIViewController *>*)items;
/**
* 修改基本配置
*/
- (void)updateWithConfig:(void(^)(YPSegmentControllerConfig *config))block;
@end
Installation
YPSegmentController is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod "YPSegmentController"