iOS 自定义UIPickerView天数选择器视图 —— HERO博客

自定义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




  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值