【问题标题】:Move specific UITextField when the Keyboard show键盘显示时移动特定的 UITextField
【发布时间】:2015-05-22 09:00:42
【问题描述】:

当小键盘出现时,我按照 Apple 文档向上移动了一个文本字段。 代码工作正常我的问题是我需要将一个特定的文本字段移向另一个,而不是实现代码 Apple 我选择的每个文本字段都向上移动......我该如何移动特定的文本字段而不是全部?

非常感谢,我插入下面使用的代码

-(void)viewWillAppear:(BOOL)animated 
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = changePasswordTextField.superview.frame;
    bkgndRect.size.height -= kbSize.height;
    [scrollView setContentOffset:CGPointMake(0.0, changePasswordTextField.frame.origin.y+kbSize.height) animated:YES];
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {


    [scrollView setContentOffset:CGPointZero animated:YES];
}

【问题讨论】:

    标签: ios8 keyboard xcode6 uitextfield


    【解决方案1】:

    您可以通过以下步骤实现您的功能。

    1. 设置 UITextField 的委托。
    2. 实现textFieldDidBeginEditing 方法,该方法将在为文本字段打开键盘时调用。因此,您可以在此方法中更改文本字段的框架,如下所示。

      -(void)textFieldDidBeginEditing:(UITextField *)textField{
           [textField setFrame:CGRectMake(0.0, textField.frame.origin.y-VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
           // VALUE = textfield you want to move upward vertically
      }
      
    3. 现在,要处理键盘隐藏事件,您可以在textFieldDidEndEditing 方法中将文本字段的框架设置为其原点,如下所示。

      - (void)textFieldDidEndEditing:(UITextField *)textField{
            [textField setFrame:CGRectMake(0.0, textField.frame.origin.y+VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
            // VALUE = textfield you want to move downward vertically
      }
      

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-03-02
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      相关资源
      最近更新 更多