美文网首页程序员
iOS复选框控件

iOS复选框控件

作者: 吴德馨 | 来源:发表于2019-03-07 15:53 被阅读0次

我们经常会碰到多按钮的情况,但是UISegmentedControl 的样式又不符合UI设计的风格,但是如果我们自己在界面上写多个按钮,那按钮点击的逻辑处理就很恶心。所以本人使用 UICollectionView 集成了一个按钮复选框控件,初始化的时候设置好需要的属性,不需要额外的逻辑代码,通过代理方法就可以获取选中的信息了。

可以轻松设置如下样式的复选按钮组:


image.png
基本使用:
//初始化
- (SliderControl *)chooseGuestTypeControl {
    if (!_chooseGuestTypeControl) {
        NSMutableArray *titles = [[NSMutableArray alloc] initWithObjects:@"本日将到", @"在住客人", nil];
        _chooseGuestTypeControl = [[[NSBundle mainBundle] loadNibNamed:@"SliderControl" owner:nil options:nil] firstObject];
        _chooseGuestTypeControl.titles = titles;
        _chooseGuestTypeControl.sliderControlType = SliderControlWithBottomTint;
        _chooseGuestTypeControl.sliderItemWidthType = SliderItemEqualWidth;
        _chooseGuestTypeControl.delegate = self;
        _chooseGuestTypeControl.sliderBackgroundColor = [UIColor whiteColor];
        _chooseGuestTypeControl.frame = self.chooseGuestTypeContainer.bounds;
    }
    return _chooseGuestTypeControl;
}

//添加到父视图上
    [self.chooseGuestTypeContainer addSubview:self.chooseGuestTypeControl];
#pragma mark ---SliderControlDelegate
- (void)sliderControl:(SliderControl *)sliderControl choosePartAtIndex:(NSInteger)index {
//代理方法内处理业务
}
SliderControl头文件展示

@class SliderControl;
//控件是否带图的 type
typedef NS_ENUM(NSInteger, SliderControlType) {
    SliderControlWithBottomTint = 0,//无图模式,底部色条
    SliderControlWithImage = 1//有图模式
};
//控件按钮是否等宽的 type
typedef NS_ENUM(NSInteger, SliderItemWidthType) {
    SliderItemWidthCustom = 0,//
    SliderItemEqualWidth = 1 //适用于标题等分父视图宽度的情况
};

@protocol SliderControlDelegate <NSObject>
@required
- (void)sliderControl:(SliderControl *)sliderControl choosePartAtIndex:(NSInteger)index;
@end

@interface SliderControl : UIView
@property (assign, nonatomic) SliderControlType sliderControlType;
@property (assign, nonatomic) SliderItemWidthType sliderItemWidthType;
@property (assign, nonatomic) id<SliderControlDelegate> delegate;

@property (nonatomic, strong) NSMutableArray<NSString *> *titles;//标题数组
@property (nonatomic, assign) BOOL canRepeatSelect;//是否可以重复点击
@property (nonatomic, assign) NSInteger selectRow;//选中的按钮下标

@property (nonatomic, strong) UIColor *cellTitleColor;//选中按钮标题颜色
@property (nonatomic, strong) UIColor *cellUnselectedTitleColor;//未选中按钮标题颜色
@property (nonatomic, strong) UIColor *cellSelectedBackgroundColor;//选中按钮的背景色
@property (nonatomic, strong) UIColor *sliderBackgroundColor;//控件背景色,也是未选择按钮的背景色

//以下属性设置需要在sliderControlType 为SliderControlWithImage才生效
@property (nonatomic, strong) NSString *selectedImageName;//选中的图片
@property (nonatomic, strong) NSString *unselectedImageName;//未选中的图片
@property (nonatomic) CGSize cellImageSize;//图片尺寸

//更换标题的方法
- (void)changeTileAtIndex:(int)index withTitle:(NSString *)title;

@end

最后,大家如果觉得对你有用,可以来我的 GitHub 下载源码,如有不足,还请提出哦!

//
//  清风明月时常有
//  Created by Cry on 2019/3/17.
//  Copyright © 2019年 foxhis. All rights reserved.
//

相关文章

  • iOS复选框控件

    我们经常会碰到多按钮的情况,但是UISegmentedControl 的样式又不符合UI设计的风格,但是如果我们自...

  • m3

    Check View(检查控件)--CheckBox(复选框),Switch(开关控件),ToggleButton...

  • CheckBox复选框

    CheckBox复选框 在Activity中初始化checkBox控件,通过setCheckedCHangeLis...

  • 零基础学鸿蒙编程-UI控件_CheckBox

    什么是CheckBox CheckBox是用于显示复选框的UI控件. 基础样例 1.普通复选框 效果图 代码 2....

  • Jquery iCheck

    表单复选框、单选框控件美化插件 主要作用为: 渲染并美化当前页面的复选框或单选框 响应复选框或单选框的点击事件 页...

  • 漂亮刷新控件-iOS

    漂亮刷新控件-iOS 漂亮刷新控件-iOS

  • UI--UIButton

    前言:UI控件整理之UIButton 一、显示图片(复选框) UIButton *button = [UIButt...

  • 【文章预热】

    预热1:你能分清楚【单选框】和【复选框】吗? 预热2:你知道【开关控件】和【复选框】的区别吗? 预热3:界面中的标...

  • 读书笔记—《交互设计精髓》第21章 控件

    1、命令控件 ①按钮 ②图标按钮 ③超链接 2、选择控件 ①复选框 ②滚翻按钮:一种应该避免的选择习惯用法,如:音...

  • 什么是控件、组件、框架

    1、控件: 任意打开一个App或者一个网页,应该很常见输入框、按钮、单选框、复选框等等的控件,控件表示程序设计中最...

网友评论

    本文标题:iOS复选框控件

    本文链接:https://www.haomeiwen.com/subject/jzncpqtx.html