簡體   English   中英

在UITextFields之間切換

[英]Switching between UITextFields

我有一個帶有附加UIView的視圖,該視圖實際上是2個UITextFields的容器

當這些文本字段中的任何一個成為第一響應者時,我需要將其上移,因為打開鍵盤時,文本字段不可見。 我正在處理UIKeyboardDidShowNotificationUIKeyboardDidHideNotification並使用以下文本字段更改容器視圖的框架:

#pragma mark-鍵盤通知處理

- (void) keyboardIsShown:(NSNotification *)notification {
    // Moving up text field while keyboard is opened

    CGRect containerFrame = self.viewContainerCredentials.frame;
    containerFrame.origin.y -= kCredentialsViewOffset;

    [UIView beginAnimations:@"moveUp" context:nil];
    [UIView setAnimationDuration:0.5f];
    self.viewContainerCredentials.frame = containerFrame;
    [UIView commitAnimations];
}

- (void) keyboardIsHidden:(NSNotification *)notification {
    // Moving down text field while keyboard is closed

    CGRect containerFrame = self.viewContainerCredentials.frame;
    containerFrame.origin.y += kCredentialsViewOffset;

    [UIView beginAnimations:@"moveDown" context:nil];
    [UIView setAnimationDuration:0.5f];
    self.viewContainerCredentials.frame = containerFrame;
    [UIView commitAnimations];
}

當我激活一個文本字段時-一切工作正常,並且當我關閉鍵盤時-視圖容器也正確向下移。

但是,當我點擊第一個字段並向上移動視圖容器,然后點擊並激活第二個文本字段而不關閉鍵盤時,我的視圖容器將還原其初始框架並返回到鍵盤下方。

有人可以幫忙嗎? 為什么會發生?

先感謝您。

UPD:問題更深:在錯誤配置了自動布局后,視圖容器在第一次響應者每次辭職后被推回。

而不是手動執行此操作。 使用TPKeyboardAvoidingScrollView 它易於使用。

首先使用UIScrollView並將所有視圖放入其中。

要與UITableViewController類一起使用, TPKeyboardAvoidingTableView.mTPKeyboardAvoidingTableView .h放入您的項目中,並使UITableView成為TPKeyboardAvoidingTableView中的TPKeyboardAvoidingTableView。 如果您的控制器沒有使用xib,我不知道有什么簡單的方法可以使其UITableView成為自定義類:阻力最小的路徑是為其創建xib。

對於非UITableViewControllers ,將TPKeyboardAvoidingScrollView.mTPKeyboardAvoidingScrollView.h源文件拖放到您的項目中,將UIScrollView彈出到視圖控制器的xib中,將滾動視圖的類設置為TPKeyboardAvoidingScrollView ,然后將所有控件放入該滾動視圖中。 您也可以以編程方式創建它,而無需使用xib-只需將TPKeyboardAvoidingScrollView用作頂層視圖即可。

建議您使用TPKeyboardAvoiding,因為它可以處理與帶有文本字段的鍵盤有關的所有此類問題。 您可以從項目中的cocoapods安裝它,然后在整個應用程序中無縫使用它。

我們可以使用UITextField Delegate方法來制作這種動畫。

當UITextfield成為FirstResponder時,這就是我們用動畫移動視圖(y = 218)的方法。

在這里,我將兩個文本字段放在一個視圖中。 當任何文本字段變為FirstResponder時,我會將視圖向上移動,即y = 100。 當用戶觸摸屏幕上的任意位置或敲擊鍵盤上的返回鍵時,將視圖移至其初始位置。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self moveViewUp];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   [textField resignFirstResponder];
   [self moveViewDown];

   return YES;
}

-(void)moveViewUp
{
   __block CGRect rect=_vwForTextFields.frame;
   [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{

    if (rect.origin.y==218)
    {
        rect.origin.y=100;
        _vwForTextFields.frame=rect;

    }

   } completion:^(BOOL finished) {
    NSLog(@"View moved up");
   }];

}

-(void)moveViewDown
{
   __block CGRect rect=_vwForTextFields.frame;
   [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{

    if (rect.origin.y==100)
    {
        rect.origin.y=218;
        _vwForTextFields.frame=rect;

    }

   } completion:^(BOOL finished) {
    NSLog(@"View moved down");
   }];
}

//To dismiss keyboard when touched anywhere on screen
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   [self.view endEditing:YES];
   [self moveViewDown];

}

嗨鍵盤通知方法(keyboardIsShown)將在未顯示鍵盤時起作用,這是在您單擊文本字段鍵盤彈出窗口時首先顯示的。 當您切換到下一個文本字段時,鍵盤已經存在,因此不會再次調用該方法。 如果您希望使用當前實現來移動文本字段,則可以為文本字段設置委托,並在viewcontroller中實現此委托方法。

//This will be called every time you click on the textfield. You can implement the method for moving the view up here   
    - (void)textFieldDidBeginEditing:(UITextField *)textField;


//Called when you have moved out of the textfield. You can implement when the keyboard is hiding.

    - (void)textFieldDidEndEditing:(UITextField *)textField; 

問題更加嚴重-為視圖容器配置了錯誤的自動布局,該布局在系統使第一響應者對主視圖進行任何其他控制后被系統推回。

我遇到了同樣的問題,這篇帖子救了我。 在我閱讀有關自動布局問題的更新后,我意識到我的問題正是這個,因此我將這行代碼添加到我的代碼中keyboardIsShown方法的第一行,以便禁用自動布局,然后再次在keyboardIsHidden中將其激活。

在keyboardIsShown中: view.TranslatesAutoresizingMaskIntoConstraints = true;

在keyboardIsHidden中: view.TranslatesAutoresizingMaskIntoConstraints = false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM