2017-09-08 50 views

回答

0
override func viewDidLoad() { 
     super.viewDidLoad() 

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

} 


func keyboardWillShow(notification: NSNotification) { 

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 

      self.view.frame.origin.y = -keyboardSize.height/2 
    } 
} 

func keyboardWillHide(notification: NSNotification) { 

    if let _ = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 

      self.view.frame.origin.y = 0 
    } 
} 
+0

沒有解決方案嗎?即時通訊實現你的代碼,但仍然顯示相同的問題 –

+0

你可以告訴我你的代碼? – Rahuld

+0

你確定等待會告訴你...但我在哪裏告訴你我的代碼? –

0

試試這個代碼,這會爲工作斯威夫特3。在這種情況下,我給出了4個textFields

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate { 

    @IBOutlet weak var textField4: UITextField! 
    @IBOutlet weak var textField3: UITextField! 
    @IBOutlet weak var textField2: UITextField! 
    @IBOutlet weak var textField1: UITextField! 

    override func viewWillDisappear(_ animated: Bool) 
    { 
    view.endEditing(true) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.textField1.delegate = self 
     self.textField2.delegate = self 
     self.textField3.delegate = self 
     self.textField4.delegate = self 

     self.textField1.tag = 0 
     self.textField2.tag = 1 
     self.textField3.tag = 2 
     self.textField4.tag = 3 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     return true 
    } 

    func textFieldDidBeginEditing(_ textField: UITextField) { 
     switch textField.tag { 
     case 0: 
      moveTextField(textField: textField, moveDistance: -120, up: true) 
     case 1: 
      moveTextField(textField: textField, moveDistance: -140, up: true) 
     case 2: 
      moveTextField(textField: textField, moveDistance: -160, up: true) 
     case 3: 
      moveTextField(textField: textField, moveDistance: -180, up: true) 
     default: 
      print("Do Nothing...!") 
     } 
    } 

    func textFieldDidEndEditing(_ textField: UITextField) { 
     switch textField.tag { 
     case 0: 
      moveTextField(textField: textField, moveDistance: -120, up: false) 
     case 1: 
      moveTextField(textField: textField, moveDistance: -140, up: false) 
     case 2: 
      moveTextField(textField: textField, moveDistance: -160, up: false) 
     case 3: 
      moveTextField(textField: textField, moveDistance: -180, up: false) 
     default: 
      print("Do Nothing...!") 
     } 
    } 


    func moveTextField(textField: UITextField, moveDistance: Int, up: Bool) 
    { 
     let moveDuration = 0.3 
     let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance) 

     UIView.beginAnimations("animateTextField", context: nil) 
     UIView.setAnimationBeginsFromCurrentState(true) 
     UIView.setAnimationDuration(moveDuration) 
     self.view.frame = (self.view.frame).offsetBy(dx: 0, dy: movement) 
     UIView.commitAnimations() 
    } 
} 
+0

對不起,@Grecory威爾遜Pullyattu你不明白我的問題,我在我的Webview中使用UIWebView和UITextField .....所以我如何實現你的代碼? –

+0

你可以添加你的textField任何地方,並實現這個代碼。它會工作。但是此代碼僅適用於4個文本字段。你可以添加更多的文本字段也可以通過添加更多的案例 –

+0

只是嘗試它。我希望它會工作 –

相關問題