自定义键盘 扩展UITextField 代码实现点击背景取消键盘

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    textField = [[UITextField alloc]init];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.frame = CGRectMake(10, 10, 150, 30);
    textField.placeholder = @"input";
    textField.delegate = self;
    textField.userInteractionEnabled = YES;
   // textField.inputView = self.inputView;    //inputView属性需手动设置 inputAccessroyView属性由系统调用
    [self.view addSubview:textField];
}


//在键盘上附加工具栏

-(UIView *)inputAccessoryView 
{
    NSLog(@"11");
    CGRect accessFrame = CGRectMake(0, 0, 768, 44);
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:accessFrame];
    
    UIBarButtonItem *buttonSmile = [[UIBarButtonItem alloc]initWithTitle:@":)" style:UIBarButtonItemStyleBordered target:self action:@selector(sendSmile:)];
    
    UIBarButtonItem *buttonSad = [[UIBarButtonItem alloc]initWithTitle:@":(" style:UIBarButtonItemStyleBordered target:self action:@selector(sendSad:)];
    
    [toolbar setItems:[NSArray arrayWithObjects:buttonSmile,buttonSad, nil]];
    return toolbar;
}


//自定义键盘

-(UIView *)inputView 
{
    NSLog(@"22");
    CGRect accessFrame = CGRectMake(0, 0, 768, 44);
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:accessFrame];
    
    UIBarButtonItem *buttonSmile = [[UIBarButtonItem alloc]initWithTitle:@":)" style:UIBarButtonItemStyleBordered target:self action:@selector(sendSmile:)];
    
    UIBarButtonItem *buttonSad = [[UIBarButtonItem alloc]initWithTitle:@":(" style:UIBarButtonItemStyleBordered target:self action:@selector(sendSad:)];
    
    [toolbar setItems:[NSArray arrayWithObjects:buttonSmile,buttonSad, nil]];
    return toolbar;
}

-(void)sendSmile:(UITextField *)textfield
{
    textField.text = [self.textField.text stringByAppendingString:@":)"];
}

-(void)sendSad:(UITextField *)textfield
{
    self.textField.text = [self.textField.text stringByAppendingString:@":("];
}


//点击背景隐藏键盘

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textField resignFirstResponder];
}

你可能感兴趣的:(自定义键盘 扩展UITextField 代码实现点击背景取消键盘)