ViewController.h
@interface ViewController : UIViewController<UITextFieldDelegate>{
UITextField * myDemoTextFiled;
UIButton * myDemoButton;
UIView *myTextFieldStatusView;
}
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myDemoButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 450, 100, 50)];
[myDemoButton setBackgroundColor:[UIColor purpleColor]];
[myDemoButton setTitle:@"Button" forState:UIControlStateNormal];
[myDemoButton setTitle:@"PRESSED" forState:UIControlStateHighlighted];
[self.view addSubview:myDemoButton];
myDemoTextFiled= [[UITextField alloc]initWithFrame:CGRectMake( 50, 500 , 200, 50)];
[myDemoTextFiled setBackgroundColor:[UIColor grayColor]];
myDemoTextFiled.font = [UIFont fontWithName:@"Times new Roman" size:28];
myDemoTextFiled.adjustsFontSizeToFitWidth = YES;
myDemoTextFiled.placeholder = @"helloEDA";
myDemoTextFiled.clearsOnInsertion = YES;
// myDemoTextFiled.keyboardType = UIKeyboardTypeNumberPad;
myDemoTextFiled.clearButtonMode = UITextFieldViewModeAlways;
myDemoTextFiled.delegate = self;
[self.view addSubview:myDemoTextFiled];
myTextFieldStatusView = [[UIView alloc] initWithFrame:CGRectMake(50, 400, 200, 10)];
myTextFieldStatusView .backgroundColor = [UIColor redColor];
[self.view addSubview:myTextFieldStatusView];
NSNotificationCenter *myDemoNotiCenter = [NSNotificationCenter defaultCenter];
[myDemoNotiCenter addObserver:self selector:@selector(NotiTest:) name:UIKeyboardWillShowNotification object:nil];
}
-(void)NotiTest:(NSNotification *)Notes{
NSDictionary *myUserinfo = [Notes userInfo];
NSValue *aValue =[myUserinfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keybroadRect =[aValue CGRectValue];
int height = keybroadRect.size.height;
NSLog(@"%d", height);
self.view.transform = CGAffineTransformMakeTranslation(0, -200);// self.view.frame.size.height;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
self.view.transform = CGAffineTransformMakeTranslation(0, 0);
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
myTextFieldStatusView.backgroundColor = [UIColor yellowColor];
// sleep(2);
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
myTextFieldStatusView.backgroundColor = [UIColor blueColor];
// sleep(2);
return YES;
}
-(BOOL)textFieldShouldClear:(UITextField *)textField{
myTextFieldStatusView.backgroundColor = [UIColor greenColor];
// sleep(2);
return YES;
}