#import "ViewController.h"
#import "LhbToolBar.h"
@interface ViewController ()<LhbToolBarDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//更换键盘 inputView就是键盘
UIView *cusTomKeyBoardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
cusTomKeyBoardView.backgroundColor = [UIColor lightGrayColor];
CGFloat btnWith = (self.view.frame.size.width-10*4)/3;
for (NSInteger i=0; i<10; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:[NSString stringWithFormat:@"%zd",i] forState:UIControlStateNormal];
btn.frame = CGRectMake(10+(btnWith+10)*(i/3), 10+60*(i%3),btnWith, 50);
[btn setBackgroundColor:[UIColor orangeColor]];
[cusTomKeyBoardView addSubview:btn];
}
self.textField.inputView = cusTomKeyBoardView;
//工具条
//实现方式一:设置键盘的inputAccessoryView(辅助条、工具条)
UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(accessoryViewBtnClick)];
[accessoryView addGestureRecognizer:tap];
//可以在这个accessoryView中放一些btn等控件,来实现工具条的功能
accessoryView.backgroundColor = [UIColor purpleColor];
// self.textField.inputAccessoryView = accessoryView;
//实现方式二:toolBar
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
UIButton *oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn.frame = CGRectMake(10, 5, 60, 40);
[oneBtn setBackgroundColor:[UIColor purpleColor]];
[oneBtn setTitle:@"下一个" forState:UIControlStateNormal];
[oneBtn addTarget:self action:@selector(ontBtnDown) forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:oneBtn];
// self.textField.inputAccessoryView = toolBar;
// 实现方式三:xib toolBar
LhbToolBar *lhbToolBar = [LhbToolBar creatToolBar];
lhbToolBar.toolBarDelegate = self;
self.textField.inputAccessoryView = lhbToolBar;
}
#pragma mark - toolBar的代理方法
- (void)toolBarNextClick:(LhbToolBar *)toolBar
{
NSLog(@"下一个");
}
- (void)toolBarUpClick:(LhbToolBar *)toolBar
{
NSLog(@"上一个");
}
- (void)toolbarDoneClick:(LhbToolBar *)toolBar
{
NSLog(@"完成");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)accessoryViewBtnClick
{
NSLog(@"工具条被点击");
}
- (void)ontBtnDown
{
NSLog(@"下一个被点击");
}
///继承子UIToolBar的xibtoolBar
#import <UIKit/UIKit.h>
@class LhbToolBar;
@protocol LhbToolBarDelegate <NSObject>
- (void)toolBarNextClick:(LhbToolBar *)toolBar;
- (void)toolBarUpClick:(LhbToolBar *)toolBar;
- (void)toolbarDoneClick:(LhbToolBar *)toolBar;
@end
//不想写三个代理方法,也可以写一个代理方法,把iteam搞成枚举
//typedef enum {
// LhbToolBarIteamNext,
// LhbToolBarIteamUp,
// LhbToolBarIteamDone
//}LhbToolBarIteam;
@interface LhbToolBar : UIToolbar
@property (nonatomic,weak) id <LhbToolBarDelegate> toolBarDelegate;
+ (LhbToolBar *)creatToolBar;
@end
@end
#import "LhbToolBar.h"
@implementation LhbToolBar
- (IBAction)next:(id)sender {
if ([self.toolBarDelegate respondsToSelector:@selector(toolBarNextClick:)]) {
[self.toolBarDelegate toolBarNextClick:self];
}
}
- (IBAction)up:(id)sender {
if ([self.toolBarDelegate respondsToSelector:@selector(toolBarUpClick:)]) {
[self.toolBarDelegate toolBarUpClick:self];
}
}
- (IBAction)done:(id)sender {
if ([self.toolBarDelegate respondsToSelector:@selector(toolbarDoneClick:)]) {
[self.toolBarDelegate toolbarDoneClick:self];
}
}
+ (LhbToolBar *)creatToolBar
{
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}