您的位置:首页 > 移动开发 > IOS开发

iOS 之点击背景退出键盘

2013-07-06 22:14 357 查看
注释: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(50,50,
200, 60)];



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



self.text.backgroundColor = [UIColorgrayColor];



//将对象添加到视图上


[self.viewaddSubview:self.text];

//调用点击背景方法



[selftapGesture];
}

-(void)tapGesture
{

//创建一个点击对象,并将其关联一个点击方法
UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapResign)];



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


tapGestureRecognizer.numberOfTapsRequired =1.0;



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


[self.viewaddGestureRecognizer:tapGestureRecognizer];



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


[tapGestureRecognizersetCancelsTouchesInView:NO];


}

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

-(void)tapResign
{


[self.text resignFirstResponder];


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: