今天写了个记录账单的demo,里面需要用到自定义键盘,这里简单的记录一下,先上个gif图
新建一个继承UIView的类DCAccountKeyBoard
.h里面
#import
NS_ASSUME_NONNULL_BEGIN
//创建自定义键盘协议
@protocol My_KeyBoardDelegate
//创建协议方法
@required//必须执行的方法
- (void)numberKeyBoard:(NSInteger) number;
- (void)cancelKeyBoard;
- (void)finishKeyBoard;
- (void)periodKeyBoard;
- (void)timeKeyBoard;
@optional//不必须执行方法
@end
@interface DCAccountKeyBoard : UIView
{
@private//私有的协议方法
id _delegate;
}
@property (nonatomic, strong) id delegate;
@property (nonatomic,strong) UIButton *timeBtn;
- (id)initWithNumber:(NSNumber *)number;
@end
NS_ASSUME_NONNULL_END
.m里面创建自定义控件,根据自己需要布局
#import "DCAccountKeyBoard.h"
@implementation DCAccountKeyBoard
-(id)initWithNumber:(NSNumber *)number {
if (self = [super init]) {
self.backgroundColor = DCColor(244, 245, 244);
self.frame = CGRectMake(0, ScreenHeight - 150, ScreenHeight, 150);
[self initKeyBoardNumber_1];
}
return self;
}
/**
setUpUI
*/
- (void)initKeyBoardNumber_1 {
self.frame=CGRectMake(0, ScreenHeight-bottomSafeHeight-203, ScreenWidth, 203);
CGFloat kWidth = ScreenWidth/4;
CGFloat kHeight = 50;
int space=1;
//number 1-9
for (int i=0; i<9; i++) {
NSString *str=[NSString stringWithFormat:@"%d",i+1];
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
if (i<3) {
button.frame=CGRectMake(i%3*kWidth+space,i/3*(kHeight+space), kWidth-space, kHeight);
}
else{
button.frame=CGRectMake(i%3*kWidth+space,i/3*(kHeight+space), kWidth-space, kHeight);
}
button.backgroundColor=[UIColor whiteColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font=[UIFont systemFontOfSize:24];
[button setTitle:str forState:UIControlStateNormal];
button.tag=i+1;
[button addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
//点
UIButton *dian=[UIButton buttonWithType:UIButtonTypeSystem];
dian.frame=CGRectMake(space,(kHeight+1)*3 , kWidth-space, kHeight);
dian.backgroundColor=[UIColor whiteColor];
[dian setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
dian.titleLabel.font=[UIFont systemFontOfSize:24];
[dian addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[dian setTitle:@"." forState:UIControlStateNormal];
dian.tag=11;
[self addSubview:dian];
// 0
UIButton *ling=[UIButton buttonWithType:UIButtonTypeSystem];
ling.frame=CGRectMake(dian.right+space,dian.y, kWidth-space, kHeight);
ling.backgroundColor=[UIColor whiteColor];
[ling setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
ling.titleLabel.font=[UIFont systemFontOfSize:24];
[ling setTitle:@"0" forState:UIControlStateNormal];
ling.tag=0;
[ling addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:ling];
//时间按钮
self.timeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
self.timeBtn.frame=CGRectMake(kWidth*3+space,0, kWidth-1, kHeight*2+space);
self.timeBtn.backgroundColor=[UIColor whiteColor];
[self.timeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.timeBtn setTitle:@"今天" forState:UIControlStateNormal];
self.timeBtn.tag=12;
[self.timeBtn addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.timeBtn];
//删除键
UIButton *delete=[UIButton buttonWithType:UIButtonTypeSystem];
delete.frame=CGRectMake(ling.right+space,ling.y, kWidth-1, kHeight);
[delete addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
delete.tag=10;
[delete setImage:IMAGENAME(@"键盘删除") forState:UIControlStateNormal];
delete.backgroundColor = [UIColor whiteColor];
[self addSubview:delete];
//完成键
UIButton *confirm=[UIButton buttonWithType:UIButtonTypeSystem];
confirm.frame=CGRectMake(delete.right+space,self.timeBtn.bottom+space, kWidth-1, kHeight*2+space);
confirm.backgroundColor=BlueColor;
[confirm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// confirm.titleLabel.font=[UIFont systemFontOfSize:20];
[confirm setTitle:@"完 成" forState:UIControlStateNormal];
[confirm addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
confirm.tag=13;
[self addSubview:confirm];
}
#pragma 键盘点击按钮事件
- (void)keyBoardAciont:(UIButton *)sender {
UIButton* btn = (UIButton*)sender;
NSInteger number = btn.tag;
if (nil == _delegate) {
DCLog(@"button tag [%ld]",(long)number);
return;
}
if (number <=9 && number >= 0) {
[_delegate numberKeyBoard:number];
return;
}
if (number == 10) {
[_delegate cancelKeyBoard];
return;
}
if (11==number) {
[_delegate periodKeyBoard];
return;
}
if (12==number) {
[_delegate timeKeyBoard];
return;
}
if (13==number) {
[_delegate finishKeyBoard];
return;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
引用自定义键盘
在控制器里面创建一个按钮点击事件,自定义一个UITextField
-(void)setUpInputView {
self.inputView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 40)];
self.inputView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.inputView];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
titleLabel.text = @"备注:";
titleLabel.font = FONT(16);
titleLabel.backgroundColor = [UIColor whiteColor];
[self.inputView addSubview:titleLabel];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(titleLabel.right, 0, self.inputView.width - titleLabel.width - 2, 40)];
textField.backgroundColor = [UIColor whiteColor];
textField.text = @"0";
textField.font = FONT(20);
self.inputTextField = textField;
[self.inputView addSubview:textField];
}
点击按钮的时候调用下面的方法
/**
UIKeyBoardAction--键盘处理
*/
-(void)showKeyBoard {
[self.inputTextField becomeFirstResponder];
self.inputTextField.text = nil;
[self keyBoardTypeAction];
}
- (void)keyBoardTypeAction {
self.my_keyboard = [[DCAccountKeyBoard alloc] initWithNumber:@1];
self.inputTextField.inputView = self.my_keyboard;
self.my_keyboard.delegate = self;
[self.inputTextField reloadInputViews];
}
- (void)hidenKeyBoard {
[self.inputTextField resignFirstResponder];
}
键盘的代理方法,按钮点击事件
- (void)numberKeyBoard:(NSInteger)number {
NSString *str = self.inputTextField.text;
self.inputTextField.text = [NSString stringWithFormat:@"%@%ld",str,(long)number];
}
- (void)cancelKeyBoard {
NSMutableString *muStr = [[NSMutableString alloc] initWithString:self.inputTextField.text];
if (muStr.length <= 0) {
return;
}
[muStr deleteCharactersInRange:NSMakeRange([muStr length] - 1, 1)];
self.inputTextField.text = muStr;
}
#pragma 输入点
-(void)periodKeyBoard {
if ([self.inputTextField.text isEqualToString:@""]) {
return;
}
//判断当前时候存在一个点
if ([self.inputTextField.text rangeOfString:@"."].location == NSNotFound) {
//输入中没有点
NSMutableString *mutableString=[[NSMutableString alloc]initWithFormat:@"%@%@",self.inputTextField.text,@"."];
self.inputTextField.text=mutableString;
}
}
-(void)timeKeyBoard {
[self hidenKeyBoard];
DCDatePickerView *pickerDate = [[DCDatePickerView alloc]initWithIsisAddYetSelect:NO isShowDay:YES];
[pickerDate show];
__weak typeof(self) weakself = self;
pickerDate.block = ^(NSString *timeString) {
DCLog(@"timeString%@",timeString);
[weakself showKeyBoard];
[weakself.my_keyboard.timeBtn setTitle:timeString forState:UIControlStateNormal];
};
}
-(void)finishKeyBoard{
[self hidenKeyBoard];
DCLog(@"type:%@-money-%@-time:%@",self.selectedTitle,self.inputTextField.text,self.my_keyboard.timeBtn.titleLabel.text);
}
viewDidLoad里面设置通知
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyBoard)];
[self.view addGestureRecognizer:tap];
[NOTIFICATION addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[NOTIFICATION addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// 根据键盘状态,调整_mainView的位置
- (void)keyboardWillShow:(NSNotification *)notification{
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyBoardSize = value.CGRectValue.size;
self.inputView.frame = CGRectMake(0, ScreenHeight - keyBoardSize.height - 40, ScreenWidth, 40);
}
-(void)keyboardWillHide:(NSNotification *)notification {
self.inputView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 40);
}
❗️如果项目中引用了IQKeyboardManagr
的话,最好在当前控制器里面设置一下,不然会出现textField和弹出来的键盘直接有个空隙,类似下图
解决方法:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[IQKeyboardManager sharedManager] setEnable:NO];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[IQKeyboardManager sharedManager] setEnable:YES];
}
想要demo的童鞋,请移步到demo传送门。。如果帮助到你了,或者喜欢的童鞋请留个✨哦