簡體   English   中英

使用鍵盤ios上下移動視圖

[英]Move View up/down with Keyboard ios

我正在使用類似於whatsapp等的聊天應用程序。它在視圖控制器中具有tableview,底部工具欄中具有文本字段和按鈕。 我遇到了有關向上滑動視圖的各種問題,並使用此鏈接設法使視圖向上滑動。 但是我想關閉鍵盤,視圖下降並適合屏幕。我嘗試使用點擊手勢並單擊返回按鈕,但似乎無濟於事。 如何使視圖向下滑動並且鍵盤消失?

此外,我如何更改文本字段的寬度,以便在用戶編寫消息時出現多行?

您可以將輕擊手勢事件添加到表格視圖單元格,也可以在用戶單擊表格視圖時使用觸摸事件方法,然后根據鍵盤的先前狀態可以顯示或隱藏鍵盤。 希望這對你有幫助。

使用textFieldShouldReturn退出第一響應者狀態(關閉鍵盤)並向上滑動視圖。

我個人是這樣的:

  1. 我注冊了通知,以了解何時顯示鍵盤以及何時隱藏鍵盤。

  2. 當鍵盤出現時,我將視圖插圖設置為包括鍵盤的大小。

  3. 向上滑動視圖

  4. 當鍵盤消失時,我將插圖設置為零。

輕按“返回”按鈕時隱藏鍵盤的TextField Delegate方法

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
    [textField resignFirstResponder];
    return NO; // We do not want the UITextField to insert line-breaks.
}

注冊鍵盤出現/消失通知

- (void)viewDidLoad
{
    ...
    // Register for notifications for when the keyboard will appear and disappear 
    [self registerForKeyboardNotifications];
}

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[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.
// Original code for this part here: http://stackoverflow.com/a/16044603/4518324
- (void)keyboardWasShown:(NSNotification *)note 
{
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.view.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
    } completion:nil];
}

- (void)keyboardWillBeHidden:(NSNotification *)note 
{
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.view.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
    } completion:nil];
}

我創建了示例代碼,其中涉及在顯示或關閉鍵盤時重新調整視圖的大小。

https://github.com/gingofthesouth/KeyboardHideShow

我說對了。 當關閉鍵盤時,我有另一種方法被調出,該方法符合View.frame-keyboard.frame.height的要求。 不管怎么說,多謝拉!:)

暫無
暫無

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

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