2017-11-18 209 views
0

我正在嘗試編寫代碼以確保當鍵盤出現時UITextField向上移動。我已經根據我在堆棧溢出中找到的代碼編寫了代碼,但它不起作用。原始代碼是用Objective-C編寫的,但我不熟悉它,因此決定用swift編寫代碼。鍵盤出現時向上移動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

嗨Rishu,我想在swift中編寫代碼..加上我寫了代碼,但textfield不動了它隱藏在鍵盤後面...我不是爲什麼? –

+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); 
} 

} 
相關問題