自定义UIPickerView天数选择器视图 ,首先看一下效果图:
下面贴上相关代码:
ViewController:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "HWDaysPicker.h"
#define mainW [UIScreen mainScreen].bounds.size.width
#define mainH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITextFieldDelegate, HWDaysPickerDelegate>
@property (nonatomic, strong) HWDaysPicker *daysPicker;
@property (nonatomic, strong) UITextField *daysTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
//创建控件
[self creatControl];
}
- (void)creatControl
{
//textField
_daysTextField = [[UITextField alloc] initWithFrame:CGRectMake(mainW * 0.05, mainW * 0.72, mainW * 0.9, mainW * 0.12)];
_daysTextField.background = [UIImage imageNamed:@"textFieldBj"];
_daysTextField.textAlignment = NSTextAlignmentRight;
_daysTextField.placeholder = @"请设置天数 ";
_daysTextField.delegate = self;
UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, mainW * 0.4, mainW * 0.12)];
lab2.textAlignment = NSTextAlignmentLeft;
lab2.text = @" 天数";
lab2.textColor = [UIColor grayColor];
_daysTextField.leftView = lab2;
_daysTextField.leftViewMode = UITextFieldViewModeAlways;
UILabel *lab22 = [[UILabel alloc] initWithFrame:CGRectMake(mainW * 0.12 - 15, 0, 15, mainW * 0.12)];
_daysTextField.rightView = lab22;
_daysTextField.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:_daysTextField];
}
//天数选择器
- (HWDaysPicker *)daysPicker
{
if (_daysPicker == nil) {
_daysPicker = [[HWDaysPicker alloc] initWithFrame:CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5)];
_daysPicker.delegate = self;
[self.view addSubview:_daysPicker];
}
return _daysPicker;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (_daysPicker.frame.origin.y != mainH && _daysPicker != nil) {
[_daysPicker dismiss];
return NO;
}else if (textField == _daysTextField) {
[self.daysPicker show];
return NO;
}
return YES;
}
#pragma mark - HWDaysPickerDelegate
- (void)daysPickerView:(HWDaysPicker *)daysPicker didSelectRowWithDays:(NSString *)days
{
_daysTextField.text = days;
}
@end
HWDaysPicker:
#import <UIKit/UIKit.h>
@class HWDaysPicker;
@protocol HWDaysPickerDelegate <NSObject>
/**
* HWDaysPicker选中代理事件
*
* @param daysPicker HWDaysPicker
* @param days 选中的天数
*/
- (void)daysPickerView:(HWDaysPicker *)daysPicker didSelectRowWithDays:(NSString *)days;
@end
@interface HWDaysPicker : UIView
@property (nonatomic, weak) id<HWDaysPickerDelegate> delegate;
- (void)show;
- (void)dismiss;
@end
#import "HWDaysPicker.h"
#define mainW [UIScreen mainScreen].bounds.size.width
#define mainH [UIScreen mainScreen].bounds.size.height
@interface HWDaysPicker ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) UIPickerView *daysPicker;
@property (nonatomic, strong) NSArray *daysArray;
@end
@implementation HWDaysPicker
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
//背景框
UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
back.image = [UIImage imageNamed:@"daysPickerBj"];
[self addSubview:back];
//天数选择器(这里用initWithFrame会导致pickerView高度为系统默认高度,无法改变)
_daysPicker = [[UIPickerView alloc] init];
_daysPicker.frame = CGRectMake(10, 10, self.frame.size.width - 20, 120);
_daysPicker.delegate = self;
_daysPicker.dataSource = self;
[self addSubview:_daysPicker];
_daysArray = @[@"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39", @"40", @"41", @"42", @"43", @"44", @"45"];
//确定按钮
UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - mainW * 0.36) * 0.5, self.frame.size.height * 0.747, mainW * 0.36, mainW * 0.11)];
[sureBtn setImage:[UIImage imageNamed:@"sureBtn"] forState:UIControlStateNormal];
[sureBtn addTarget:self action:@selector(sureBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:sureBtn];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
//初始化位置
[_daysPicker selectRow:8 inComponent:0 animated:NO];
[self pickerView:_daysPicker didSelectRow:8 inComponent:0];
}
#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _daysArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _daysArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (_delegate && [_delegate respondsToSelector:@selector(daysPickerView:didSelectRowWithDays:)]) {
[_delegate daysPickerView:self didSelectRowWithDays:_daysArray[row]];
}
}
- (void)sureBtnOnClick
{
[self dismiss];
}
- (void)show
{
[UIView animateWithDuration:0.3f animations:^{
self.frame = CGRectMake(mainW * 0.05, mainH - mainW * 0.75, mainW * 0.9, mainW * 0.5);
}];
}
- (void)dismiss
{
[UIView animateWithDuration:0.3f animations:^{
self.frame = CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5);
}];
}
@end