2017-07-26 62 views
-1

我有一個ScrollView其中有TableViews。我設置鍵盤出現時,鍵盤上方的內容應該移動,所以鍵盤不會遮擋任何東西。只有當鍵盤彈起時,我如何滾動鍵盤上方的內容?如何在鍵盤顯示時在Tabelview中滾動?

回答

0

當鍵盤上下移動時,您需要管理高度UITableView。通知將有助於管理各種鍵盤的狀態。根據國家,我們可以udpate UITableView的高度

-(void) keyboardWillShow:(NSNotification *)note{ 
    // get keyboard size and loctaion 
    CGRect keyboardBounds; 
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds]; 
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; 
    // Need to translate the bounds to account for rotation. 
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil]; 
    // animations settings 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:[duration doubleValue]]; 
    [UIView setAnimationCurve:[curve intValue]]; 
    // set views with new info 
    _containerBottom.constant = keyboardBounds.size.height; 
    CGRect rectTable = _tblMessages.frame; 
    rectTable.size.height -= keyboardBounds.size.height; 
    _tblMessages.frame = rectTable; 
    [self.view layoutIfNeeded]; 
    [self scrollToBottom]; 

    // commit animations 
    [UIView commitAnimations]; 
} 

-(void) keyboardWillHide:(NSNotification *)note{ 

    CGRect keyboardBounds; 
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds]; 
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil]; 

    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; 
    // animations settings 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:[duration doubleValue]]; 
    [UIView setAnimationCurve:[curve intValue]]; 


    CGRect rectTable = _tblMessages.frame; 
    rectTable.size.height += keyboardBounds.size.height; 
    _tblMessages.frame = rectTable; 
    // commit animations 
    [UIView commitAnimations]; 
} 
    - (void)viewWillAppear:(BOOL)animated 
    { 
     [super viewWillAppear:animated]; 
     // register for keyboard notifications 
      [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

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

    } 

    - (void)viewWillDisappear:(BOOL)animated 
    { 
     [super viewWillDisappear:animated]; 
     // unregister for keyboard notifications while not visible. 
     [[NSNotificationCenter defaultCenter] removeObserver:self 
               name:UIKeyboardWillShowNotification 
               object:nil]; 

     [[NSNotificationCenter defaultCenter] removeObserver:self 
               name:UIKeyboardWillHideNotification 
               object:nil]; 
    } 
0

您可以添加添加觀察者來檢查鍵盤顯示與否。

NotificationCenter.default.addObserver(self, 
    selector: #selector(viewController.keyboardWillShow(notification:)), 
    name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

NotificationCenter.default.addObserver(self, 
    selector: #selector(viewController.keyboardWillHide(notification:)), 
    name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

func keyboardWillShow(notification: NSNotification) { 
    if let keyboardSize = (notification.userInfo? [UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
    print("Show") 
    } 
} 

func keyboardWillHide(notification: NSNotification) { 
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
    print("Hide") 
    } 
} 

然後你需要爲內容做計算表明,當鍵盤將會出現,並設置內容滾動型的偏移量。

相關問題