iOS 之点击背景退出键盘

原博地址:iOS 之点击背景退出键盘

注释:UITextFiled才有输入框,所以先建立一个UITextFiled对象,点击他会出现输入框,利用UITapGestureRescognizer 类实现操作,上代码:

.h文件中不用操作

//

//  LYXViewController.h

//  UITap

//

//  Created by liyongxing on 13-7-6.

//  Copyright (c) 2013 liyongxing. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface LYXViewController : UIViewController


@end


在.m中创建对象

//

//  LYXViewController.m

//  UITap

//

//  Created by liyongxing on 13-7-6.

//  Copyright (c) 2013 liyongxing. All rights reserved.

//


#import "LYXViewController.h"


@interface LYXViewController ()


@property (nonatomic,strong) UITextField * text;


@end


@implementation LYXViewController


-(void)viewDidLoad

{

    [super viewDidLoad];

    

    //创建UITextField的对象,并给他定位制定大小,位置

    

    self.text = [[UITextField alloc]initWithFrame:CGRectMake(505020060)];

    

    //设置背景色,默认为透明色

    

    self.text.backgroundColor = [UIColor grayColor];

    

    //将对象添加到视图上

    

    [self.view addSubview:self.text];


    //调用点击背景方法

    

    [self tapGesture];

}


-(void)tapGesture

{

    //创建一个点击对象,并将其关联一个点击方法

    UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(tapResign)];

    

    //设置点击多少次退出键盘,一般设置为1

    

    tapGestureRecognizer.numberOfTapsRequired = 1.0;

    

    //将点击对象添加到当前视图

    

    [self.view addGestureRecognizer:tapGestureRecognizer];

    

    //是否取消点击背景视图的动作,设置为否

    

    [tapGestureRecognizer setCancelsTouchesInView:NO];

    

}


//点击背景视图的时候发生的时间,退出第一响应者,到此就能完成我们想要的效果


-(void)tapResign

{

    

    [self.text resignFirstResponder];

    

}




- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


转自:http://blog.csdn.net/u011199592/article/details/9260613


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值