话不多说上图效果就是这样,嘿嘿嘿。接下来分析一下,层次结构。首先是segment有一个背景view,颜色为白色,在它之上加入了五个button。然后在button下面有一个可以移动的横线,横线的宽度是根据button上文字的长度来变化的,颜色和button选中时一样,同时选中时可以切换不同的页面。上代码分析一下。- (void)setupTitlesButtonWithTitlesArray:(NSArray *)titlesArray { CGFloat titlesButtonW = self.frame.size.width / titlesArray.count; CGFloat titlesButtonH = self.frame.size.height; for (NSUInteger i = 0; i titlesArray.count; i ++) { CYButton *titlesButton = [[CYButton alloc] initWithFrame:CGRectMake(i * titlesButtonW, 0, titlesButtonW, titlesButtonH)]; [titlesButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; [titlesButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; [self addSubview:titlesButton]; self.titlesButton = tit