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
相關文章
相關標籤/搜索