2017-07-28 51 views
1

我不知道爲什麼我試圖讓滾動視圖向上移動文本,所以他們仍然可見時,當鍵盤通過堆棧溢出反應一直沒有奏效,但他們不適合我。將下面的蘋果文檔翻譯成Swift對我來說應該足以讓它起作用,但我不知道Objective C足夠完成這項工作。有人願意將以下內容翻譯成Swift 3嗎?無法得到任何滾動textview當從鍵盤呈現從堆棧溢出工作的反應

1.// 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]; 
} 

2. // Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
scrollView.contentInset = contentInsets; 
scrollView.scrollIndicatorInsets = contentInsets; 

// If active text field is hidden by keyboard, scroll it so it's visible 
// Your app might not need or want this behavior. 
CGRect aRect = self.view.frame; 
aRect.size.height -= kbSize.height; 
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
    [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; 
    } 
} 

3.// Called when the UIKeyboardWillHideNotification is sent 
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification 
    { 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 

回答

1

在這裏,你去。不過,我已經在UIView中使用過這段代碼。您應該能夠對scrollview進行這些調整。

func addKeyboardNotifications() { 
     NotificationCenter.default.addObserver(self, 
               selector: #selector(keyboardWillShow(notification:)), 
               name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.addObserver(self, 
               selector: #selector(keyboardWillHide(notification:)), 
               name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    } 

    func keyboardWillShow(notification: NSNotification) { 

     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { 
      let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double 
// if using constraints    
// bottomViewBottomSpaceConstraint.constant = keyboardSize.height 
self.view.frame.origin.y -= keyboardSize.height 
      UIView.animate(withDuration: duration) { 
       self.view.layoutIfNeeded() 
      } 
     } 
    } 
    func keyboardWillHide(notification: NSNotification) { 

     let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double 
//if using constraint 
//  bottomViewBottomSpaceConstraint.constant = 0 
self.view.frame.origin.y += keyboardSize.height 
     UIView.animate(withDuration: duration) { 
      self.view.layoutIfNeeded() 
     } 
    } 

不要忘記在正確的地方刪除通知。

func removeKeyboardNotifications() { 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
}