原作者:https://www.jianshu.com/p/00186b02cb04
1.新增一个EBDropdownListView.m,内容如下:
#import "EBDropdownListView.h"
@implementation EBDropdownListItem
- (instancetype)initWithItem:(NSString*)itemId itemName:(NSString*)itemName {
self = [super init];
if (self) {
_itemId = itemId;
_itemName = itemName;
}
return self;
}
- (instancetype)init {
return [self initWithItem:nil itemName:nil];
}
@end
@interface EBDropdownListView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIImageView *arrowImg;
@property (nonatomic, strong) UITableView *tbView;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, copy) EBDropdownListViewSelectedBlock selectedBlock;
@end
static CGFloat const kArrowImgHeight= 10;
static CGFloat const kArrowImgWidth= 15;
static CGFloat const kTextLabelX = 5;
static CGFloat const kItemCellHeight = 40;
@implementation EBDropdownListView
#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupView];
[self setupProperty];
}
return self;
}
- (instancetype)initWithDataSource:(NSArray*)dataSource {
_dataSource = dataSource;
return [self initWithFrame:CGRectZero];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self setupFrame];
}
#pragma mark - Setup
- (void)setupProperty {
_textColor = [UIColor blackColor];
_font = [UIFont systemFontOfSize:14];
_selectedIndex = 0;
_textLabel.font = _font;
_textLabel.textColor = _textColor;
UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapViewExpand:)];
[_textLabel addGestureRecognizer:tapLabel];
UITapGestureRecognizer *tapImg = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapViewExpand:)];
[_arrowImg addGestureRecognizer:tapImg];
}
- (void)setupView {
[self addSubview:self.textLabel];
[self addSubview:self.arrowImg];
}
- (void)setupFrame {
CGFloat viewWidth = CGRectGetWidth(self.bounds)
, viewHeight = CGRectGetHeight(self.bounds);
_textLabel.frame = CGRectMake(kTextLabelX, 0, viewWidth - kTextLabelX - kArrowImgWidth , viewHeight);
_arrowImg.frame = CGRectMake(CGRectGetWidth(_textLabel.frame), viewHeight / 2 - kArrowImgHeight / 2, kArrowImgWidth, kArrowImgHeight);
}
#pragma mark - Events
-(void)tapViewExpand:(UITapGestureRecognizer *)sender {
[self rotateArrowImage];
CGFloat tableHeight = _dataSource.count * kItemCellHeight;
UIWindow * window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self.backgroundView];
[window addSubview:self.tbView];
// 获取按钮在屏幕中的位置
CGRect frame = [self convertRect:self.bounds toView:window];
CGFloat tableViewY = frame.origin.y + frame.size.height;
CGRect tableViewFrame;
tableViewFrame.size.width = frame.size.width;
tableViewFrame.size.height = tableHeight;
tableViewFrame.origin.x = frame.origin.x;
if (tableViewY + tableHeight < CGRectGetHeight([UIScreen mainScreen].bounds)) {
tableViewFrame.origin.y = tableViewY;
}else {
tableViewFrame.origin.y = frame.origin.y - tableHeight;
}
_tbView.frame = tableViewFrame;
UITapGestureRecognizer *tagBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewDismiss:)];
[_backgroundView addGestureRecognizer:tagBackground];
}
-(void)tapViewDismiss:(UITapGestureRecognizer *)sender {
[self removeBackgroundView];
}
#pragma mark - Methods
- (void)setDropdownListViewSelectedBlock:(EBDropdownListViewSelectedBlock)block {
_selectedBlock = block;
}
- (void)setViewBorder:(CGFloat)width borderColor:(UIColor*)borderColor cornerRadius:(CGFloat)cornerRadius {
self.layer.cornerRadius = cornerRadius;
self.layer.borderColor = borderColor.CGColor;
self.layer.borderWidth = width;
}
- (void)rotateArrowImage {
// 旋转箭头图片
_arrowImg.transform = CGAffineTransformRotate(_arrowImg.transform, M_PI);
}
- (void)removeBackgroundView {
[_backgroundView removeFromSuperview];
[_tbView removeFromSuperview];
[self rotateArrowImage];
}
- (void)selectedItemAtIndex:(NSInteger)index {
_selectedIndex = index;
if (index < _dataSource.count) {
EBDropdownListItem *item = _dataSource[index];
_selectedItem = item;
_textLabel.text = item.itemName;
}
}
#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.font = _font;
cell.textLabel.textColor = _textColor;
EBDropdownListItem *item = _dataSource[indexPath.row];
cell.textLabel.text = item.itemName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self selectedItemAtIndex:indexPath.row];
[self removeBackgroundView];
if (_selectedBlock) {
_selectedBlock(self);
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataSource.count;
}
#pragma mark - Setter
- (void)setDataSource:(NSArray *)dataSource {
_dataSource = dataSource;
if (dataSource.count > 0) {
[self selectedItemAtIndex:_selectedIndex];
}
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
[self selectedItemAtIndex:selectedIndex];
}
- (void)setFont:(UIFont *)font {
_font = font;
_textLabel.font = font;
}
- (void)setTextColor:(UIColor *)textColor {
_textColor = textColor;
_textLabel.textColor = textColor;
}
#pragma mark - Getter
- (UILabel*)textLabel {
if (!_textLabel) {
_textLabel = [UILabel new];
_textLabel.userInteractionEnabled = YES;
}
return _textLabel;
}
- (UIImageView*)arrowImg {
if (!_arrowImg) {
_arrowImg = [UIImageView new];
_arrowImg.image = [UIImage imageNamed:@"dropdownFlag"];
_arrowImg.userInteractionEnabled = YES;
}
return _arrowImg;
}
- (UITableView*)tbView {
if (!_tbView) {
_tbView = [UITableView new];
_tbView.dataSource = self;
_tbView.delegate = self;
_tbView.tableFooterView = [UIView new];
_tbView.backgroundColor = [UIColor whiteColor];
_tbView.layer.shadowOffset = CGSizeMake(4, 4);
_tbView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
_tbView.layer.shadowOpacity = 0.8;
_tbView.layer.shadowRadius = 4;
_tbView.layer.borderColor = [UIColor grayColor].CGColor;
_tbView.layer.borderWidth = 0.5;
_tbView.clipsToBounds = NO;
_tbView.rowHeight = kItemCellHeight;
}
return _tbView;
}
- (UIView*)backgroundView {
if (!_backgroundView) {
_backgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
return _backgroundView;
}
@end
2.新增一个EBDropdownListView.h,内容如下:
#import <UIKit/UIKit.h>
@interface EBDropdownListItem : NSObject
@property (nonatomic, copy, readonly) NSString *itemId;
@property (nonatomic, copy, readonly) NSString *itemName;
- (instancetype)initWithItem:(NSString*)itemId itemName:(NSString*)itemName NS_DESIGNATED_INITIALIZER;
@end
@class EBDropdownListView;
typedef void (^EBDropdownListViewSelectedBlock)(EBDropdownListView *dropdownListView);
@interface EBDropdownListView : UIView
// 字体颜色,默认 blackColor
@property (nonatomic, strong) UIColor *textColor;
// 字体默认14
@property (nonatomic, strong) UIFont *font;
// 数据源
@property (nonatomic, strong) NSArray *dataSource;
// 默认选中第一个
@property (nonatomic, assign) NSUInteger selectedIndex;
// 当前选中的DropdownListItem
@property (nonatomic, strong, readonly) EBDropdownListItem *selectedItem;
- (instancetype)initWithDataSource:(NSArray*)dataSource;
- (void)setViewBorder:(CGFloat)width borderColor:(UIColor*)borderColor cornerRadius:(CGFloat)cornerRadius;
- (void)setDropdownListViewSelectedBlock:(EBDropdownListViewSelectedBlock)block;
@end
ViewController.m文件内容
#import "ViewController.h"
#import "EBDropdownListView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 300, 20)];
msgLabel.font = [UIFont systemFontOfSize:14];
[self.view addSubview:msgLabel];
EBDropdownListItem *item1 = [[EBDropdownListItem alloc] initWithItem:@"1" itemName:@"item1"];
EBDropdownListItem *item2 = [[EBDropdownListItem alloc] initWithItem:@"2" itemName:@"item2"];
EBDropdownListItem *item3 = [[EBDropdownListItem alloc] initWithItem:@"3" itemName:@"item3"];
EBDropdownListItem *item4 = [[EBDropdownListItem alloc] initWithItem:@"4" itemName:@"item4"];
// 弹出框向上
EBDropdownListView *dropdownListView = [[EBDropdownListView alloc] initWithDataSource:@[item1, item2, item3, item4]];
dropdownListView.frame = CGRectMake(20, 100, 130, 30);//设置下拉框的位置
dropdownListView.selectedIndex = 2;
[dropdownListView setViewBorder:0.5 borderColor:[UIColor grayColor] cornerRadius:2];
[self.view addSubview:dropdownListView];
[dropdownListView setDropdownListViewSelectedBlock:^(EBDropdownListView *dropdownListView) {
NSString *msgString = [NSString stringWithFormat:
@"selected name:%@ id:%@ index:%ld"
, dropdownListView.selectedItem.itemName
, dropdownListView.selectedItem.itemId
, dropdownListView.selectedIndex];
msgLabel.text = msgString;
}];
// 弹出框向下
EBDropdownListView *dropdownListView1 = [EBDropdownListView new];
dropdownListView1.dataSource = @[item1, item2, item3, item4];
dropdownListView1.frame = CGRectMake(20, CGRectGetHeight(self.view.frame) - 50, 130, 30);
dropdownListView1.selectedIndex = 1;
[dropdownListView1 setViewBorder:0.5 borderColor:[UIColor grayColor] cornerRadius:2];
[self.view addSubview:dropdownListView1];
[dropdownListView1 setDropdownListViewSelectedBlock:^(EBDropdownListView *dropdownListView) {
NSString *msgString = [NSString stringWithFormat:
@"selected name:%@ id:%@ index:%ld"
, dropdownListView.selectedItem.itemName
, dropdownListView.selectedItem.itemId
, dropdownListView.selectedIndex];
msgLabel.text = msgString;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end