正文
(!self.myTitleArray) {
self.myTitleArray=@[@
"新闻"
,@
"NBA"
,@
"财经"
,@
"科技"
,@
"软件公司"
,@
"健身"
,@
"优秀文摘"
];
}
self.items=[[NSMutableArray alloc]init];
self.itemsWidth=[[NSArray alloc]init];
//初始化滚动
if
(!self.myScrollView) {
self.myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, NAV_TAB_BAR_Width, NAV_TAB_BAR_HEIGHT)];
self.myScrollView.backgroundColor=[UIColor redColor];
self.myScrollView.showsHorizontalScrollIndicator = NO;
self.myScrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.myScrollView];
}
//赋值跟计算滚动
_itemsWidth = [self getButtonsWidthWithTitles:self.myTitleArray];
CGFloat contentWidth = [self contentWidthAndAddNavTabBarItemsWithButtonsWidth:_itemsWidth];
self.myScrollView.contentSize = CGSizeMake(contentWidth, 0);
self.currentIndex=0;
}
/**
* @brief 计算宽度
*
* @param titles <
*
* @
return
<
*/
- (NSArray *)getButtonsWidthWithTitles:(NSArray *)titles;
{
NSMutableArray *widths = [@[] mutableCopy];
_selectedTitlesWidth = 0;
for
(NSString *title
in
titles)
{
CGSize size = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : TABBAR_TITLE_FONT} context:nil].size;
CGFloat eachButtonWidth = size.width + 20.f;
_selectedTitlesWidth += eachButtonWidth;
NSNumber *width = [NSNumber numberWithFloat:eachButtonWidth];
[widths addObject:width];
}
if