首先,需要引头文件, 签订协议
已改成 MRC !
//1.三个textfield
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 150, 40)];
textField1.layer.borderWidth = 1;
textField1.layer.cornerRadius = 10;
[self.view addSubview:textField1];
textField1.delegate = self;
[textField1 release];
UITextField *textField2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 300, 150, 40)];
textField2.layer.borderWidth = 1 ;
textField2.layer.cornerRadius =10 ;
[self.view addSubview:textField2];
textField2.delegate = self;
[textField2 release];
UITextField *textField3 = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 150, 40)];
textField3.layer.borderWidth = 1 ;
textField3.layer.cornerRadius = 10 ;
[self.view addSubview:textField3];
textField3.delegate = self;
[textField3 release];
// 2.执行协议中的方法.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//只要输入框被激活了,就会触发这个方法.
if (textField.frame.origin.y > HEIGHT /2) {
//先做一个差值
CGFloat height = textField.frame.origin.y - HEIGHT/2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y-height);
}
return YES;
}
//等到编译结束的时候,再让他回到原位,
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//整个是在移动self.view,父视图的移动会让所有的子视图一同移动,而且相对父视图的坐标位置不会发生变化,所以可以沿用上一个方法的判断.
if(textField.frame.origin.y > HEIGHT/2){
//先做一个差值
CGFloat height = textField.frame.origin.y - HEIGHT/2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + height);
}
return YES;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-15 19:39:34