2017-07-28 6 views
1

スクロールビューでテキストビューを移動しようとすると、キーボードがスタックオーバーフロー回答は機能していませんが、私のためではありません。下のリンゴの文書をSwiftに翻訳すると、これを動作させるには十分であるはずですが、これを実行するのに十分なObjective Cについてはわかりません。誰かが以下をスウィフト3に翻訳してもらえますか?キーボードのスタックオーバーフローが発生したときにスクロールするテキストビューの応答が得られない

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でこのコードを使用しました。これらの調整をスクロールビューに対して行うことができるはずです。

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) 
} 
関連する問題