在键盘上方添加取消按钮


在键盘上方添加取消按钮_第1张图片


代码直接加载appDelegate里面就可以:


-(void)hiddenKeyBorad:(id)sedner
{//取消键盘的方法
    
    UIWindow * keyWindow=[[UIApplication sharedApplication] keyWindow];
    [keyWindow endEditing:YES];
    
}
-(void)keyBoradShow:(NSNotification *)notification
{//键盘通知触发的方法
    
    NSValue * value=[notification.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] ;
    CGRect lastRect=[value CGRectValue];
    
    UIWindow * _myWindow=[[[UIApplication sharedApplication] delegate] window];
    
    UIView * vBtn=[_myWindow viewWithTag:-200];
    [vBtn removeFromSuperview];
    vBtn=nil;
    
    UIButton * btn=[UIButton buttonWithType:0];
    btn.tag=-200;
    [btn setFrame:CGRectMake(265,lastRect.origin.y-30, 55, 30)];
    [_myWindow addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"keyBorad.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(hiddenKeyBorad:) forControlEvents:UIControlEventTouchUpInside];

    
}

-(void)keyBoradHidden:(NSNotification *)notification
{
    UIWindow * _myWindow=[[[UIApplication sharedApplication] delegate] window];
    
    UIView * vBtn=[_myWindow viewWithTag:-200];
    [vBtn removeFromSuperview];
    vBtn=nil;
    
}

注册通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoradShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoradHidden:) name:UIKeyboardWillHideNotification object:nil];



你可能感兴趣的:(在键盘上方添加取消按钮)