2017-04-17 159 views
0

我有5個UITextFields,每當我點擊文本框時,鍵盤出現。當用戶觸摸文本字段外時,鍵盤將隱藏。但是,有一個特殊的textField用於彈出窗口。當出現彈出窗口時,以前的文本框無法隱藏鍵盤。我該如何隱藏鍵盤,然後顯示彈出窗口?Swift 3.0 - 隱藏多個UItextfield的鍵盤

func textFieldDidBeginEditing(_ textField: UITextField) { 
    if textField == self.customerAddress{ 
     scrollView.setContentOffset(CGPoint(x: 0,y:5), animated: true) 

    } 
    else if textField == self.district{ 
     textField.resignFirstResponder() 

     scrollView.setContentOffset(CGPoint(x: 0,y:20), animated: true) 

     visualEffectView.isHidden = false 
     districtpicker.selectRow(3, inComponent: 0, animated: false) 

     self.view.addSubview(districtPopUp) 
     districtPopUp.center = self.subView.convert(CGPoint(x:subView.frame.size.width/2,y:subView.frame.size.height/3), to: subView) 
     districtPopUp.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3) 

     UIView.animate(withDuration: 0.4, animations: { 
      self.visualEffectView.alpha = 0.5 
      self.districtPopUp.alpha = 1 
      self.districtPopUp.transform = CGAffineTransform.identity 
     }) 
    } 

} 


@IBAction func districtPopDismiss(_ sender: UIButton) { 
    scrollView.setContentOffset(CGPoint(x: 0,y:-64), animated: true) 
    UIView.animate(withDuration: 0.3, animations: { 
     self.districtPopUp.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3) 
     self.visualEffectView.alpha = 1 
    }) { (success) in 
     self.districtPopUp.removeFromSuperview() 
    } 
    self.visualEffectView.isHidden = true 
} 


func textFieldDidEndEditing(_ textField: UITextField) { 
    textField.resignFirstResponder() 
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    scrollView.setContentOffset(CGPoint(x: 0,y:-64), animated: true) 

    textField.resignFirstResponder() 

    return true 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CustomerViewController.hideKeyboard)) 

    subView.addGestureRecognizer(tapGesture) 

    visualEffectView.isHidden = true 
    self.customerName.delegate = self 
    self.customerAddress.delegate = self 
    self.customerContact.delegate = self 
    self.customerIC.delegate = self 
    self.ticketNumber.delegate = self 
    self.latitudeGPS.delegate = self 
    self.longitudeGPS.delegate = self 
    self.district.delegate = self 
    // Do any additional setup after loading the view. 
} 


func hideKeyboard(){ 
    scrollView.setContentOffset(CGPoint(x: 0,y:-64), animated: true) 

    self.customerName.resignFirstResponder() 
    self.customerAddress.resignFirstResponder() 
    self.customerContact.resignFirstResponder() 
    self.customerIC.resignFirstResponder() 
    self.ticketNumber.resignFirstResponder() 
    self.latitudeGPS.resignFirstResponder() 
    self.longitudeGPS.resignFirstResponder() 
    self.district.resignFirstResponder() 
} 

回答

0

而是在每個文本框的調用resignFirstResponder()的你可以調用view.endEditing(true)和鍵盤將隱藏。嘗試在負責呈現彈出窗口的邏輯之前調用它。

+0

喜,是的,每次用戶結束UItextfield時,我都嘗試過view.endEditing(true),但是,我無法事先調用view.endEditing(true),因爲一旦我點擊了新的uitextfield,鍵盤應用程序再次聽到。如何解決這個問題? – Yuan

0

簡易所有視圖控制器迅速3+ ​​

此代碼幫助你隱藏鍵盤觸摸上viewcontrol任何地方

extension UIViewController { 

    func hideKeyboardWhenTappedAround() { 
     let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard)) 
     tap.cancelsTouchesInView = false 
     view.addGestureRecognizer(tap) 
    } 

    @objc func dismissKeyboard() { 
     view.endEditing(true) 
    } 
} 

使用下面的代碼在您的viewDidLoad

self.hideKeyboardWhenTappedAround()