【问题标题】:Moving textView/textField to top while keyboard appearance [duplicate]在键盘外观时将 textView/textField 移动到顶部 [重复]
【发布时间】:2014-10-06 14:10:07
【问题描述】:

当我选择其中一个时,如何将textView/textField 移动到视图顶部?

【问题讨论】:

  • Google 是开发者最好的朋友。

标签: ios text textview uitextfield


【解决方案1】:

工作代码....

1) 设置delegate给你textFields

[self.textField setDelegate:self];

2) 向上移动textField

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.textField)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y - 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

3) 向下移动textField

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == self.textField)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y + 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

4) 向上移动textView

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if (textView == self.textView)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y - 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

5) 将textView 向下移动

-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView == self.textView)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y + 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-09
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多