码迷,mamicode.com
首页 > 其他好文 > 详细

UI 03 关于UITextField键盘遮挡问题

时间:2015-08-04 00:47:12      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

首先,需要引头文件, 签订协议
已改成 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UI 03 关于UITextField键盘遮挡问题

标签:

原文地址:http://blog.csdn.net/gao_zi/article/details/47264771

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!