ios登陆页以及键盘关闭demo

1、新建一个single view 的project

2、另外新建两个类(非必要) DElabel.h DETextField.h 将共用属性以及方法都放类当中,特殊属性以及方法直接放VC中

3、声明全局变量tfuser tfpass两个textfield

4、键盘关闭关键在新建一个背景,让背景触发事件让两个textfield失去控制权。

=========================== 

//

//  DEViewController.m

//  testLoginPage

//

//  Created by Decade on 14/8/18.

//  Copyright (c) 2014年 decade. All rights reserved.

//

 

#import "DEViewController.h"

 

@interface DEViewController ()

 

@end

 

@implementation DEViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    

    UIControl *bglb = [[UIControl alloc]init];

    bglb.frame = [[UIScreen mainScreen]bounds];

    [bglb addTarget:self action:@selector(bgTap) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:bglb];

    

    DElabel *lbuser = [[DElabel alloc]init];

    lbuser.frame = CGRectMake(50, 80, 50, 30);

    lbuser.text = @"帐号:";

    [self.view addSubview:lbuser];

    

    DElabel *lbpass = [[DElabel alloc]init];

    lbpass.frame = CGRectMake(50, 120, 50, 30);

    lbpass.text = @"密码:";

    [self.view addSubview:lbpass];

    

    tfuser = [[DETextField alloc]init];

    tfuser.frame = CGRectMake(100, 80, 150, 30);

    tfuser.placeholder = @"请输入帐号";

//    [tfuser addTarget:self action:@selector(bgTap) forControlEvents:UIControlEventEditingDidEndOnExit];

    [self.view addSubview:tfuser];

    

    tfpass = [[DETextField alloc]init];

    tfpass.frame = CGRectMake(100, 120, 150, 30);

    tfpass.placeholder = @"请输入密码";

    tfpass.secureTextEntry = YES;

    tfpass.keyboardType = UIKeyboardTypeNumberPad;

    [self.view addSubview:tfpass];

 

}

 

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

//    if (![self.view isExclusiveTouch]) {

//        [self.view resignFirstResponder];

//    }

//}

-(void)bgTap{

    [tfuser resignFirstResponder];

    [tfpass resignFirstResponder];

}

 

@end
原文地址:https://www.cnblogs.com/decade/p/3922802.html