iOS点击完按钮 出现 keyboard 并且屏幕上移

来源:互联网 发布:时时技巧软件 编辑:程序博客网 时间:2024/05/16 05:01

ViewController.h


@interface ViewController :UIViewController<UITextFieldDelegate>{

   UITextField * myDemoTextFiled;

   UIButton * myDemoButton;

   UIView *myTextFieldStatusView;

}



ViewController.m


- (void)viewDidLoad {

    [superviewDidLoad];

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

    myDemoButton = [[UIButtonalloc]initWithFrame:CGRectMake(50,450, 100, 50)];

    [myDemoButton setBackgroundColor:[UIColorpurpleColor]];

    [myDemoButton setTitle:@"Button"forState:UIControlStateNormal];

    [myDemoButton setTitle:@"PRESSED"forState:UIControlStateHighlighted];

    [self.viewaddSubview:myDemoButton];

    

     myDemoTextFiled= [[UITextFieldalloc]initWithFrame:CGRectMake(50, 500 , 200,50)];

    [myDemoTextFiledsetBackgroundColor:[UIColorgrayColor]];

    myDemoTextFiled.font = [UIFontfontWithName:@"Times new Roman"size:28];

    myDemoTextFiled.adjustsFontSizeToFitWidth =YES;

    myDemoTextFiled.placeholder =@"helloEDA";

    myDemoTextFiled.clearsOnInsertion =YES;

//    myDemoTextFiled.keyboardType = UIKeyboardTypeNumberPad;

    myDemoTextFiled.clearButtonMode =UITextFieldViewModeAlways;

    myDemoTextFiled.delegate =self;

    [self.viewaddSubview:myDemoTextFiled];

    

    myTextFieldStatusView = [[UIViewalloc] initWithFrame:CGRectMake(50,400, 200, 10)];

    myTextFieldStatusView .backgroundColor = [UIColorredColor];

    [self.viewaddSubview:myTextFieldStatusView];

    

    NSNotificationCenter *myDemoNotiCenter = [NSNotificationCenterdefaultCenter];

    [myDemoNotiCenter addObserver:selfselector:@selector(NotiTest:)name:UIKeyboardWillShowNotificationobject:nil];

}


-(void)NotiTest:(NSNotification *)Notes{

   NSDictionary *myUserinfo = [Notes userInfo];

    NSValue *aValue =[myUserinfoobjectForKey: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 {

    [superdidReceiveMemoryWarning];

    

}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    self.view.transform =CGAffineTransformMakeTranslation(0,0);

    return YES;

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    myTextFieldStatusView.backgroundColor = [UIColoryellowColor];

//    sleep(2);

    return YES;

}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    myTextFieldStatusView.backgroundColor = [UIColorblueColor];

//    sleep(2);

    return YES;

}

-(BOOL)textFieldShouldClear:(UITextField *)textField{

    myTextFieldStatusView.backgroundColor = [UIColorgreenColor];

//    sleep(2);

    return YES;

}



0 0
原创粉丝点击