84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
想实现如图的uisegmentedcontrol效果,没有边框、未选中的时候下划线是浅色的,选中是黑色的实线。请高手指点,谢谢!(如果不用图片的话)
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
If you have comments, just post the code directly
#import @protocol CJNaviViewDelegate - (void)D_selectedTag:(NSInteger)tag; @end @interface CJNaviView : UIView - (instancetype)initWithNumberOfTitles:(NSArray *)titles andFrame:(CGRect)frame delegate:(id)delegate; @end CJNaviView.m #import "CJNaviView.h" #define kSelectedColor [UIColor grayColor] #define kNormalColor [UIColor lightGrayColor] // Button进行封装 @interface CJNaviButton:UIButton @property (nonatomic, weak) UIView *lineView; @end @implementation CJNaviButton - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { CGFloat lineWidth = 3; UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - lineWidth, frame.size.width, lineWidth)]; // 设置初始状态 lineView.backgroundColor = kNormalColor; // lineView.hidden = YES; _lineView = lineView; [self setTitleColor:kNormalColor forState:UIControlStateNormal]; self.titleLabel.font = [UIFont systemFontOfSize:14]; [self setBackgroundColor:[UIColor whiteColor]]; [self addSubview:lineView]; } return self; } @end @interface CJNaviView() @property (nonatomic, weak) id delegate; @property (nonatomic, strong) CJNaviButton *lastClickButton; @end @implementation CJNaviView /** * init方法 * * @param titles title数组 :@[@"选项1",@"选项2"] * @param frame 整个naviView frame * @param delegate 设置代理 * * @return CJNaviView实例 */ - (instancetype)initWithNumberOfTitles:(NSArray *)titles andFrame:(CGRect)frame delegate:(id)delegate{ if (self = [super initWithFrame:frame]) { // 设置代理 self.delegate = delegate; CGFloat buttonWidth = SCREENSIZE.width / titles.count; for (int i = 0; i < titles.count; i ++) { CJNaviButton *button = [[CJNaviButton alloc] initWithFrame:CGRectMake(i *buttonWidth, 0, buttonWidth, frame.size.height)]; // 默认选中第一个 设置状态 if (i == 0) { [button setTitleColor:kSelectedColor forState:UIControlStateNormal]; button.lineView.backgroundColor = kSelectedColor; // 保留为上次选择中的button _lastClickButton = button; } // 设置对应的tag button.tag = i; [button setTitle:titles[i] forState:UIControlStateNormal]; [button addTarget:self action:@selector(A_choosed:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } } return self; } - (void)A_choosed:(CJNaviButton *)button{ // 连续点击同一个不响应回调 if (_lastClickButton != button) { // 设置状态 [button setTitleColor:kSelectedColor forState:UIControlStateNormal]; button.lineView.backgroundColor = kSelectedColor; [_lastClickButton setTitleColor:kNormalColor forState:UIControlStateNormal]; _lastClickButton.lineView.backgroundColor = kNormalColor; _lastClickButton = button; // 回调 可用block if ([self.delegate respondsToSelector:@selector(D_selectedTag:)]) { [self.delegate D_selectedTag:button.tag]; } } }
The uisegmentedcontrol provided by the system has too little customizable content. In my projects, I usually use UIView + UIButton to achieve similar effects
If you have comments, just post the code directly
The uisegmentedcontrol provided by the system has too little customizable content. In my projects, I usually use UIView + UIButton to achieve similar effects