2017-11-18 11 views
0

キーボードが存在するときにUITextFieldが上に移動するようにするコードを記述しようとしています。私はスタックオーバーフローで見つかったものに基づいてコードを書いていますが、それは動作しません。元のコードはobjective-cで書かれていましたが、私はそれに精通していないので、迅速にコードを書くことに決めました。キーボードが現れたときにUITextFieldを上に移動

誰かが私がここで執着しているかもしれないと言うことができますか?

{ 

     // moving text box up when tyoing 
func registerForKeyboardNotifications() 
{ 
    //Adding notifies on keyboard appearing 
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 



@objc func keyboardWasShown(notification: NSNotification) 
{ 
    //Need to calculate keyboard exact size due to Apple suggestions 

    self.scrollView.isScrollEnabled = true 
    let info : NSDictionary = notification.userInfo! as NSDictionary 
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size 
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0) 

    self.scrollView.contentInset = contentInsets 
    self.scrollView.scrollIndicatorInsets = contentInsets 

    var aRect : CGRect = self.view.frame 
    aRect.size.height -= keyboardSize!.height 
    if (yourEmail) != nil 
    { 
     if (!aRect.contains(yourEmail!.frame.origin)) 
     { 
      self.scrollView.scrollRectToVisible(yourEmail!.frame, animated: true) 
     } 
    } 


} 


@objc func keyboardWillBeHidden(notification: NSNotification) 
{ 
    //Once keyboard disappears, restore original positions 
    let info : NSDictionary = notification.userInfo! as NSDictionary as NSDictionary 
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size 
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0) 
    self.scrollView.contentInset = contentInsets 
    self.scrollView.scrollIndicatorInsets = contentInsets 
    self.view.endEditing(true) 
    self.scrollView.isScrollEnabled = false 

} 

func textFieldDidBeginEditing(_ textField: UITextField) 
{ 
    yourEmail = textField 
} 

func textFieldDidEndEditing(_ textField: UITextField) 
{ 
    yourEmail = nil 
} 





} 
+0

可能な重複[キーボードが存在する場合のUITextFieldが上がるようにする方法?](https://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) –

+0

こんにちは麗水、私は迅速にコードを記述しようとしています..プラス私はコードを書いているが、テキストフィールドはいつでも動かないそれはキーボードの後ろに隠れています...私はなぜそうではありませんか? –

+0

あなたのテキストフィールドはスクロールビュー内にありますか? –

答えて

0

このコードの下

func textFieldDidBeginEditing(textField: UITextField!) 
{ 
    self.scrollView.setContentOffset(CGPoint.init(x: 0, y: scrollBy), animated: true) 
    // scrollBy - pass the height you want your scrollview to be scrolled when keyboard appears 
} 

func textFieldDidEndEditing(textField: UITextField!) 
{ 
    self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true) 

    self.view.endEditing(true); 
} 
0

を試してみて、私は提案の助けを借りて書いたコードです:

{ 

func textFieldDidBeginEditing(_ textField: UITextField) { 

    if textField.frame.maxY > self.view.frame.height * 0.6 
    { 
     self.scrollView.setContentOffset(CGPoint.init(x: 0, y: textField.frame.maxY - self.view.frame.height * 0.6 + 2.0), animated: true) 

    } 
    else{ 
     return 
    } 

} 

func textFieldDidEndEditing(_ textField: UITextField) 
{ 
    self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true) 

    self.view.endEditing(true); 
} 

} 
関連する問題