TextField-===注册登陆随着键盘的弹出而上移动(方法1)

#import "ViewController.h"

#define kScreenSize [UIScreen mainScreen].bounds.size
#define kDebugPrint NSLog(@"%s %d",__func__,__LINE__)


@interface ViewController ()  <UITextFieldDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTextFlied];
}
- (void)creatTextFlied {
    NSArray *arr = @[@"登录",@"注册"];
    for (NSInteger i = 0; i < arr.count; i++) {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 30+40*i, kScreenSize.width-20, 30)];
        textField.borderStyle = UITextBorderStyleLine;
        textField.tag = 101+i;
        if (i == 1) {
            textField.secureTextEntry = YES;//密文
        }
        
        textField.clearButtonMode = UITextFieldViewModeAlways;
        
        [self.view addSubview:textField];
        [textField release];
        //创建按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake((kScreenSize.width-100)*i, 350, 100, 50);
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 201+i;
        [self.view addSubview:button];
        
    }
    
    
    UITextField *text1 = (UITextField *)[self.view viewWithTag:101];
    UITextField *text2 = (UITextField *)[self.view viewWithTag:102];
    //设置代理
    text1.delegate = self;
    text2.delegate = self;

}
#pragma mark - UITextFieldDelegate协议

//点击return键 键盘通知代理调用

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    //取消第一响应
//    [textField resignFirstResponder];
    [self hiddenKeyboard];
    kDebugPrint;
    return YES;//一般返回YES  return 键是否有效
}
//将要进入编辑模式的时候调用
//决定是否可以进入编辑模式
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"将要进入编辑模式tag:%ld",textField.tag);
    return YES;//YES表示可以进入编辑模式
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"已经进入编辑模式");
    
    UIButton *button1 = (UIButton *)[self.view viewWithTag:201];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:202];
    /*下面是移动的固定写法*///====================
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    
    button1.frame = CGRectMake(0, 200, 100, 50);
    button2.frame = CGRectMake(kScreenSize.width-100, 200, 100, 50);
    [UIView commitAnimations];
    //===========================================
}

//将要结束编辑模式调用
//是否可以结束编辑模式
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"将要结束编辑模式,tag:%ld",textField.tag);
    return YES;//NO不能结束
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"已经结束编辑模式");
}

- (void)hiddenKeyboard {
    UITextField *text1 = (UITextField *)[self.view viewWithTag:101];
    UITextField *text2 = (UITextField *)[self.view viewWithTag:102];
    //取消第一响应
    [text1 resignFirstResponder];
    [text2 resignFirstResponder];
    
    
    UIButton *button1 = (UIButton *)[self.view viewWithTag:201];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:202];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    
    button1.frame = CGRectMake(0, 350, 100, 50);
    button2.frame = CGRectMake(kScreenSize.width-100, 350, 100, 50);
    [UIView commitAnimations];

    
}

//给画布增加 触摸
//点击 屏幕 触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //收键盘
    [self hiddenKeyboard];
}



- (void)btnClick:(UIButton *)button {
    kDebugPrint;
    //收键盘
    [self hiddenKeyboard];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


你可能感兴趣的:(TextField-===注册登陆随着键盘的弹出而上移动(方法1))