TCViewPager.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. //
  2. // TCViewPager.m
  3. // XiaoHaiTun
  4. //
  5. // Created by 唐天成 on 16/8/28.
  6. // Copyright © 2016年 唐天成. All rights reserved.
  7. //
  8. #import "TCViewPager.h"
  9. #import "UIView+EasyFrame.h"
  10. #define MYRGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
  11. #define DefultLeftAndRightSpace 0
  12. #define DefultTitlePageSpace 0
  13. #define EditBtnW 30
  14. @interface TCPageParam ()
  15. @property (nonatomic, assign) CGFloat width;
  16. @end
  17. @implementation TCPageParam
  18. - (instancetype)init {
  19. if(self = [super init]) {
  20. [self setupDefaultValue];
  21. }
  22. return self;
  23. }
  24. //设置默认值
  25. - (void)setupDefaultValue {
  26. self.selectIndex = 0;
  27. self.titleArray = [NSMutableArray array];
  28. self.tabSelectedBottomLineColor = [UIColor blackColor];
  29. self.showSelectedBottomLine = NO;
  30. self.selectedBottomLineScale = 1.0;
  31. self.tabTitleColor = [UIColor blackColor];
  32. self.tabSelectedTitleColor = [UIColor redColor];
  33. self.selectedLabelBigScale = 1.0;
  34. self.labelFont = [UIFont systemFontOfSize:15];
  35. self.pageHeaderHeight = 40;
  36. self.showBottomGradientLayer = YES;
  37. self.bottomGradientH = 6;
  38. self.bottomGradientColorArr = @[MYRGBACOLOR(239,242,241,1), MYRGBACOLOR(239,242,241,0.0)];
  39. self.pageHeaderControlBottomLineColor = [UIColor clearColor];
  40. self.pageHeaderControlBottomLineHeight = 1.0;
  41. self.viewPagerBgColor = [UIColor whiteColor];
  42. self.titlePageSpace = DefultTitlePageSpace;
  43. self.leftAndRightSpace = DefultLeftAndRightSpace;
  44. self.animateScroll = YES;
  45. }
  46. - (void)setTitleArray:(NSMutableArray *)titleArray {
  47. _titleArray = [NSMutableArray arrayWithArray:titleArray];
  48. }
  49. //这个方法是当用户没有传入leftAndRightSpace或者titlePageSpace,那么就按照默认的逻辑来做,
  50. /*
  51. 1.leftAndRightSpace和titlePageSpace都没有传入,那么就默认leftAndRightSpace为22,titlePageSpace为22,然后如果加上titleWidth后还是小于一屏,那么久重新计算leftAndRightSpace和titlePageSpace,使其刚好一屏
  52. 2.如果leftAndRightSpace有值,titlePageSpace无值,那么让titlePageSpace默认为22,然后如果加上titleWidth后还是小于一屏,那么久重新计算titlePageSpace,使其刚好一屏
  53. 3.如果leftAndRightSpace无值,titlePageSpace有值,那么leftAndRightSpace默认为22
  54. 4.如果leftAndRightSpace和titlePageSpace都有值,那就照着你设置的值来
  55. */
  56. - (void)calculate {
  57. CGFloat titleAllLength = 0;
  58. if(!self.titleArrayLength.count) {
  59. NSMutableArray *titleWidths = [NSMutableArray array];
  60. for(NSString *title in _titleArray) {
  61. CGFloat width = kScreenW / self.titleArray.count;
  62. [titleWidths addObject:@(width)];
  63. titleAllLength = titleAllLength + width;
  64. }
  65. self.titleArrayLength = titleWidths;
  66. }
  67. for(NSNumber *width in self.titleArrayLength) {
  68. titleAllLength = titleAllLength + width.floatValue;
  69. }
  70. if(self.leftAndRightSpace >= 0) {
  71. if(self.titlePageSpace >= 0) {
  72. } else {
  73. self.titlePageSpace = DefultTitlePageSpace;
  74. if((titleAllLength + self.leftAndRightSpace * 2 + (self.titleArray.count -1) * self.titlePageSpace) < self.width && self.titleArray.count>1) {
  75. self.titlePageSpace = (self.width - titleAllLength - self.leftAndRightSpace * 2) / (self.titleArray.count - 1);
  76. }
  77. }
  78. } else {
  79. if(self.titlePageSpace >= 0) {
  80. self.leftAndRightSpace = DefultLeftAndRightSpace;
  81. } else {
  82. self.leftAndRightSpace = DefultLeftAndRightSpace;
  83. self.titlePageSpace = DefultTitlePageSpace;
  84. if((titleAllLength + self.leftAndRightSpace * 2 + (self.titleArray.count -1) * self.titlePageSpace) < self.width) {
  85. self.leftAndRightSpace = self.titlePageSpace = (self.width - titleAllLength) / (self.titleArray.count + 1);
  86. }
  87. }
  88. }
  89. }
  90. @end
  91. @interface TCViewPager ()
  92. //分页列表
  93. @property (nonatomic, strong) UIScrollView *scrollView;
  94. //菜单标题按钮列表
  95. @property (nonatomic, strong) UIScrollView *pageHeaderControl;
  96. //菜单标题按钮数组
  97. @property (nonatomic, strong) NSMutableArray *titleBtnArray;
  98. //视图
  99. @property (nonatomic, strong) NSArray *views;
  100. @property (nonatomic, strong) UIButton *editTagBtn;
  101. //底部选中下划线
  102. @property (nonatomic, strong) UIImageView *selectedBottomLine;
  103. //// 当前选择的ViewController或者View
  104. //@property (nonatomic, strong) id currentSelectViewOrController;
  105. //点击block
  106. @property (nonatomic, copy) TC_VP_SelectedBlock block;
  107. @property (nonatomic, copy) TC_VP_EditTagBlock editTagBlock;;
  108. //配置
  109. @property (nonatomic, strong) TCPageParam *param;
  110. @property (nonatomic, assign) NSInteger currentIndex;
  111. @end
  112. @implementation TCViewPager
  113. - (UIViewController *)selectController {
  114. if(_views.count > self.currentIndex && self.currentIndex >= 0) {
  115. _selectController = _views[self.currentIndex];
  116. return _selectController;
  117. }
  118. return nil;
  119. }
  120. //按钮的点击事件
  121. - (void)tabBtnClicked:(UIButton *)sender
  122. {
  123. NSInteger index = sender.tag - 100;
  124. if(index == self.param.selectIndex) {
  125. if(self.block) {
  126. self.block(self, index,self.param.selectIndex, YES);
  127. }
  128. return;
  129. } else {
  130. [self setSelectIndex:index isClickBtn:YES];
  131. }
  132. }
  133. - (void)changeSelectedIndex:(NSInteger)selectIndex {
  134. if(selectIndex == self.param.selectIndex) {
  135. if(_block) {
  136. _block(self, selectIndex,self.param.selectIndex, NO);
  137. }
  138. return;
  139. } else {
  140. [self setSelectIndex:selectIndex isClickBtn:NO];
  141. }
  142. }
  143. //修改某一个标题的名字
  144. - (void)setTitle:(NSString *)title forSegmentAtIndex:(NSInteger)index {
  145. if(self.param.titleArray.count > index) {
  146. NSMutableArray *titleMutableArray = [NSMutableArray arrayWithArray:self.param.titleArray];
  147. titleMutableArray[index] = title;
  148. self.param.titleArray = titleMutableArray;
  149. }
  150. UIButton *btn = self.titleBtnArray[index];
  151. [btn setTitle:title forState:UIControlStateNormal];
  152. }
  153. /**
  154. 设置选择的按钮索引 触发的方法(里面主要做的就是修改下面红色label的滑动距离,并且使得上面的按钮选中状态发生改变)
  155. @param index 选中第几个按钮
  156. */
  157. - (void)setSelectIndex:(NSInteger)index isClickBtn:(BOOL)isClickBtn
  158. {
  159. for(NSInteger i = 0; i < self.views.count; i++) {
  160. UIButton *btn = (UIButton *)[self.pageHeaderControl viewWithTag:i + 100];
  161. [btn setTitleColor:self.param.tabTitleColor forState:UIControlStateNormal];
  162. btn.selected = NO;
  163. btn.transform = CGAffineTransformIdentity;
  164. }
  165. UIButton *button = (UIButton *)[self.pageHeaderControl viewWithTag:index + 100];
  166. // UIView *selectedLabel = (UIView *)[self.pageHeaderControl viewWithTag:300];
  167. button.selected = YES;
  168. button.transform = CGAffineTransformMakeScale(self.param.selectedLabelBigScale, self.param.selectedLabelBigScale);
  169. id childVc = self.views[index];
  170. if([childVc isKindOfClass:[UIViewController class]]) {
  171. // if (![childVc isViewLoaded]) {
  172. [self.scrollView addSubview:((UIViewController *)childVc).view];
  173. ((UIViewController *)childVc).view.frame = CGRectMake(index * self.width, 0, self.scrollView.width, self.scrollView.height);
  174. // }
  175. } else if([childVc isKindOfClass:[UIView class]]) {
  176. if(![self.scrollView.subviews containsObject:childVc]) {
  177. [self.scrollView addSubview:((UIView *)childVc)];
  178. ((UIView *)childVc).frame = CGRectMake(index * self.width, 0, self.scrollView.width, self.scrollView.height);
  179. }
  180. }
  181. if(floor(self.scrollView.contentOffset.x) == floor(index * self.width)) {
  182. self.selectedBottomLine.width = 20;
  183. self.selectedBottomLine.centerX = button.centerX;
  184. self.scrollView.contentOffset = CGPointMake(index * self.width, 0);
  185. //让按钮居中
  186. [self setUpTitleCenter:button];
  187. if(self.block) {
  188. self.block(self, index,self.param.selectIndex,isClickBtn);
  189. }
  190. self.param.selectIndex = index;
  191. } else {
  192. CGFloat duration = 0.3;
  193. if(!self.param.animateScroll) {
  194. duration = 0;
  195. }
  196. [UIView animateWithDuration:duration animations:^{
  197. self.selectedBottomLine.width = 20;
  198. self.selectedBottomLine.centerX = button.centerX;
  199. self.scrollView.contentOffset = CGPointMake(index * self.width, 0);
  200. //让按钮居中
  201. [self setUpTitleCenter:button];
  202. } completion:^(BOOL finished) {
  203. }];
  204. if(self.block) {
  205. self.block(self, index,self.param.selectIndex,isClickBtn);
  206. }
  207. self.param.selectIndex = index;
  208. }
  209. }
  210. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  211. {
  212. NSInteger index = scrollView.contentOffset.x/self.width;
  213. [self setSelectIndex:index isClickBtn:NO];
  214. }
  215. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  216. CGFloat curPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
  217. // 左边label角标
  218. NSInteger leftIndex = curPage;
  219. // 右边的label角标
  220. NSInteger rightIndex = leftIndex + 1;
  221. if(self.titleBtnArray.count <= leftIndex) {
  222. return;
  223. }
  224. // 获取左边的Button
  225. UIButton *leftButton = self.titleBtnArray[leftIndex];
  226. // 获取右边的button
  227. UIButton *rightButton;
  228. if (rightIndex < self.titleBtnArray.count ) {
  229. rightButton = self.titleBtnArray[rightIndex];
  230. }
  231. // 计算下右边缩放比例
  232. CGFloat rightScale = curPage - leftIndex;
  233. // 计算下左边缩放比例
  234. CGFloat leftScale = 1 - rightScale;
  235. leftButton.transform = CGAffineTransformMakeScale(leftScale * (self.param.selectedLabelBigScale - 1) + 1, leftScale * (self.param.selectedLabelBigScale - 1)+ 1);
  236. rightButton.transform = CGAffineTransformMakeScale(rightScale * (self.param.selectedLabelBigScale-1) + 1, rightScale * (self.param.selectedLabelBigScale - 1)+ 1);
  237. // 计算偏移量
  238. CGFloat offsetX = self.param.leftAndRightSpace ;
  239. if(self.param.titleArrayLength.count >= 1) {
  240. offsetX = offsetX + [self.param.titleArrayLength[0] floatValue] * 0.5;
  241. }
  242. NSInteger indexInt = ((NSInteger)(scrollView.contentOffset.x/ scrollView.width));
  243. CGFloat indexFloat = scrollView.contentOffset.x / scrollView.width - indexInt;
  244. for(NSInteger i = 0; i<indexInt; i++) {
  245. offsetX = offsetX + [self.param.titleArrayLength[i] floatValue]/2 + self.param.titlePageSpace +( self.param.titleArrayLength.count > (i+1) ? [self.param.titleArrayLength[i+1] floatValue]/2 : 0);
  246. }
  247. if(self.param.titleArrayLength.count > indexInt ) {
  248. offsetX = offsetX + ([self.param.titleArrayLength[indexInt] floatValue]/2 + self.param.titlePageSpace +( self.param.titleArrayLength.count > (indexInt+1) ? [self.param.titleArrayLength[indexInt+1] floatValue]/2 : 0)) * indexFloat;
  249. }
  250. if(rightIndex < _titleBtnArray.count) {
  251. self.selectedBottomLine.width = 20;
  252. } else {
  253. self.selectedBottomLine.width = 20;
  254. }
  255. self.selectedBottomLine.centerX = offsetX;
  256. offsetX = offsetX - self.width * 0.5;
  257. if (offsetX < 0) offsetX = 0;
  258. // 获取最大滚动范围
  259. CGFloat maxOffsetX = self.pageHeaderControl.contentSize.width - self.pageHeaderControl.width;
  260. if (offsetX > maxOffsetX) offsetX = maxOffsetX;
  261. // 滚动标题滚动条
  262. [self.pageHeaderControl setContentOffset:CGPointMake(offsetX, 0) animated:NO];
  263. CGFloat NormalTitleB, NormalTitleG, NormalTitleR, SelectedTitleB, SelectedTitleG, SelectedTitleR;
  264. [self.param.tabTitleColor getRed:&NormalTitleR green:&NormalTitleG blue:&NormalTitleB alpha:nil];
  265. [self.param.tabSelectedTitleColor getRed:&SelectedTitleR green:&SelectedTitleG blue:&SelectedTitleB alpha:nil];
  266. NormalTitleB = NormalTitleB * 255;
  267. NormalTitleG = NormalTitleG * 255;
  268. NormalTitleR = NormalTitleR * 255;
  269. SelectedTitleB = SelectedTitleB * 255;
  270. SelectedTitleG = SelectedTitleG * 255;
  271. SelectedTitleR = SelectedTitleR * 255;
  272. [leftButton setTitleColor:MYRGBACOLOR(SelectedTitleR+(NormalTitleR-SelectedTitleR) * rightScale, SelectedTitleG+(NormalTitleG-SelectedTitleG) * rightScale, SelectedTitleB+(NormalTitleB-SelectedTitleB) * rightScale, 1) forState:UIControlStateNormal];
  273. [leftButton setTitleColor:MYRGBACOLOR(SelectedTitleR+(NormalTitleR-SelectedTitleR) * rightScale, SelectedTitleG+(NormalTitleG-SelectedTitleG) * rightScale, SelectedTitleB+(NormalTitleB-SelectedTitleB) * rightScale, 1) forState:UIControlStateSelected];
  274. [rightButton setTitleColor:MYRGBACOLOR(SelectedTitleR+(NormalTitleR-SelectedTitleR) * leftScale, SelectedTitleG+(NormalTitleG-SelectedTitleG) * leftScale, SelectedTitleB+(NormalTitleB-SelectedTitleB) * leftScale, 1) forState:UIControlStateNormal];
  275. [rightButton setTitleColor:MYRGBACOLOR(SelectedTitleR+(NormalTitleR-SelectedTitleR) * leftScale, SelectedTitleG+(NormalTitleG-SelectedTitleG) * leftScale, SelectedTitleB+(NormalTitleB-SelectedTitleB) * leftScale, 1) forState:UIControlStateSelected];
  276. NSLog(@"%f",curPage);
  277. }
  278. // 设置标题居中
  279. - (void)setUpTitleCenter:(UIButton *)centerButton
  280. {
  281. // 计算偏移量
  282. CGFloat offsetX = centerButton.center.x - self.width * 0.5;
  283. if (offsetX < 0) offsetX = 0;
  284. // 获取最大滚动范围
  285. CGFloat maxOffsetX = self.pageHeaderControl.contentSize.width - self.pageHeaderControl.width;
  286. if (offsetX > maxOffsetX) offsetX = maxOffsetX;
  287. // 滚动标题滚动条
  288. self.pageHeaderControl.contentOffset = CGPointMake(offsetX, 0);
  289. }
  290. - (void)didSelectedBlock:(TC_VP_SelectedBlock)block
  291. {
  292. self.block = block;
  293. }
  294. - (void)editTagBtnClickBlock:(TC_VP_EditTagBlock)block {
  295. _editTagBlock = block;
  296. }
  297. - (void)editTagBtnClick {
  298. if(_editTagBlock) {
  299. _editTagBlock();
  300. }
  301. }
  302. - (NSInteger)currentIndex {
  303. return self.param.selectIndex;
  304. }
  305. - (id)initWithFrame:(CGRect)frame
  306. views:(NSArray *)views
  307. param:(TCPageParam *)param
  308. {
  309. self = [super initWithFrame:frame];
  310. if(self) {
  311. self.backgroundColor = [UIColor whiteColor];
  312. self.views = views;
  313. self.param = param;
  314. self.param.width = frame.size.width;
  315. if(self.views.count != self.param.titleArray.count) {
  316. if(self.views.count > self.param.titleArray.count) {
  317. for(NSInteger i =0;i<self.param.titleArray.count-self.views.count;i++) {
  318. [self.param.titleArray addObject:@""];
  319. }
  320. } else {
  321. [self.param.titleArray removeObjectsInRange:NSMakeRange(self.views.count, self.param.titleArray.count-self.views.count)];
  322. }
  323. }
  324. [self.param calculate];
  325. }
  326. return self;
  327. }
  328. - (void)layoutSubviews{
  329. [super layoutSubviews];
  330. if(self.scrollView) return;
  331. CGRect rect = self.bounds;
  332. self.backgroundColor = self.param.viewPagerBgColor;
  333. self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.param.pageHeaderHeight, self.width, self.height - self.param.pageHeaderHeight)];
  334. if(@available(iOS 11.0, *)){
  335. self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
  336. } else {
  337. self.viewController.automaticallyAdjustsScrollViewInsets = NO;
  338. }
  339. self.scrollView.userInteractionEnabled = YES;
  340. self.scrollView.showsHorizontalScrollIndicator = NO;
  341. self.scrollView.showsVerticalScrollIndicator = NO;
  342. self.scrollView.pagingEnabled = YES;
  343. self.scrollView.directionalLockEnabled = YES;
  344. self.scrollView.bounces = NO;
  345. self.scrollView.backgroundColor = self.param.viewPagerBgColor;
  346. self.pageHeaderControl = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.param.canEdit ? self.width - EditBtnW : self.width, self.param.pageHeaderHeight)];
  347. self.pageHeaderControl.backgroundColor = self.param.viewPagerBgColor;
  348. self.pageHeaderControl.showsHorizontalScrollIndicator = NO;
  349. [self addSubview:self.scrollView];
  350. [self addSubview:self.pageHeaderControl];
  351. if(self.param.canEdit) {
  352. self.editTagBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-EditBtnW, 0, EditBtnW, self.param.pageHeaderHeight)];
  353. self.editTagBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 5);
  354. self.editTagBtn.backgroundColor = [UIColor clearColor];
  355. [self.editTagBtn addTarget:self action:@selector(editTagBtnClick) forControlEvents:UIControlEventTouchUpInside];
  356. [self addSubview:self.editTagBtn];
  357. [self.editTagBtn setImage:[UIImage imageNamed:@"TAG_Mger_Edit"] forState:UIControlStateNormal];
  358. }
  359. if(self.param.showBottomGradientLayer) {
  360. CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  361. NSMutableArray *colorArr = [NSMutableArray array];
  362. for(UIColor *c in self.param.bottomGradientColorArr) {
  363. [colorArr addObject:(__bridge id)c.CGColor];
  364. }
  365. gradientLayer.colors = colorArr;
  366. gradientLayer.startPoint = CGPointMake(0, 0);
  367. gradientLayer.endPoint = CGPointMake(0, 1.0);
  368. gradientLayer.frame = CGRectMake(0, self.param.pageHeaderHeight, rect.size.width, self.param.bottomGradientH);
  369. [self.layer addSublayer:gradientLayer];
  370. }
  371. for(NSInteger i = 0; i < self.views.count; i++) {
  372. CGRect pageframe = self.pageHeaderControl.frame;
  373. pageframe.size.width = [self.param.titleArrayLength[i] floatValue];
  374. pageframe.origin.x = pageframe.origin.x + self.param.leftAndRightSpace;
  375. for(NSInteger j = 0; j < i; j++) {
  376. pageframe.origin.x = pageframe.origin.x + self.param.titlePageSpace + [self.param.titleArrayLength[j] floatValue];
  377. }
  378. //创建菜单按钮
  379. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  380. button.imageView.contentMode = UIViewContentModeScaleAspectFit;
  381. [button setFrame:pageframe];
  382. button.tag = 100 + i;
  383. [button setTitleColor:self.param.tabTitleColor forState:UIControlStateNormal];
  384. [button setTitleColor:self.param.tabSelectedTitleColor forState:UIControlStateSelected];
  385. [button setBackgroundColor:[UIColor clearColor]];
  386. id currentTitle = self.param.titleArray[i];
  387. if([currentTitle isKindOfClass:[NSString class]]) {
  388. [button setTitle:self.param.titleArray[i] forState:UIControlStateNormal];
  389. } else if([currentTitle isKindOfClass:[UIImage class]]){
  390. [button setImage:currentTitle forState:UIControlStateNormal];
  391. }
  392. if(!self.titleBtnArray) {
  393. self.titleBtnArray = [NSMutableArray array];
  394. }
  395. [self.titleBtnArray addObject:button];
  396. button.titleLabel.font = self.param.labelFont;
  397. [button addTarget:self action:@selector(tabBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  398. if(!i) {
  399. button.selected = YES;
  400. self.selectedBottomLine.centerX = button.centerX;
  401. }
  402. [self.pageHeaderControl addSubview:button];
  403. }
  404. //创建菜单标题scrollView下方的横线
  405. if(self.param.pageHeaderControlBottomLineWidth == 0) {
  406. self.param.pageHeaderControlBottomLineWidth = self.width;
  407. }
  408. UIView *pageHeaderControlBottomLine = [[UIView alloc] initWithFrame:CGRectMake((self.width - self.param.pageHeaderControlBottomLineWidth)/2, self.param.pageHeaderHeight - self.param.pageHeaderControlBottomLineHeight, self.param.pageHeaderControlBottomLineWidth, self.param.pageHeaderControlBottomLineHeight)];
  409. pageHeaderControlBottomLine.backgroundColor = self.param.pageHeaderControlBottomLineColor;
  410. [self.pageHeaderControl addSubview:pageHeaderControlBottomLine];
  411. if(self.param.showSelectedBottomLine) {
  412. //创建菜单按钮下划线
  413. self.selectedBottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.pageHeaderControl.height -4, 20, 4)];
  414. self.selectedBottomLine.image = [UIImage imageNamed:@"选中"];
  415. // self.selectedBottomLine.backgroundColor = self.param.tabSelectedBottomLineColor;
  416. [self.pageHeaderControl addSubview:self.selectedBottomLine];
  417. }
  418. CGFloat pageContentSizeWidth = 2 * self.param.leftAndRightSpace + (self.param.titleArrayLength.count - 1)*self.param.titlePageSpace;
  419. for(NSInteger i = 0; i < self.param.titleArrayLength.count; i++) {
  420. pageContentSizeWidth = pageContentSizeWidth + [self.param.titleArrayLength[i] floatValue];
  421. }
  422. self.pageHeaderControl.contentSize = CGSizeMake(pageContentSizeWidth >= self.pageHeaderControl.width ? pageContentSizeWidth : self.pageHeaderControl.width, 0);
  423. [self.scrollView setContentSize:CGSizeMake(rect.size.width * self.views.count + 1, 0)];
  424. self.scrollView.delegate = self;
  425. self.scrollView.contentOffset = CGPointMake(self.width*self.param.selectIndex, 0);
  426. if(self.views.count) {
  427. [self setSelectIndex:self.param.selectIndex isClickBtn:NO];
  428. }
  429. }
  430. - (void)dealloc {
  431. }
  432. -(UIViewController*)viewController
  433. {
  434. UIResponder *nextResponder = self;
  435. do
  436. {
  437. nextResponder = [nextResponder nextResponder];
  438. if ([nextResponder isKindOfClass:[UIViewController class]])
  439. return (UIViewController*)nextResponder;
  440. } while (nextResponder != nil);
  441. return nil;
  442. }
  443. @end