2016-11-11 102 views
0

當鍵盤出現在屏幕上時,我正在向上移動srollview時出現問題。 我有4個textfields在我的屏幕上,所有數字鍵盤textfields沒有下一個/完成返回鍵,所以我已添加自定義工具欄與上一個和下一個按鈕在我的鍵盤上。 所以,當我從一個文本字段移動到另一個文本字段使用下一個按鈕時,光標正確移動到下一個字段,但滾動視圖不會向上移動,所以我的文本字段隱藏在鍵盤後面。我已將scrollview添加到了我的故事板,並設置了textfield的代表。當鍵盤可見時,ScrollView不會自動向上移動

代碼:

override open func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     self.registerForKeyboardNotifications() 
     keyboardVisible = false 
    } 


    override open func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(animated) 

     self.deregisterFromKeyboardNotifications() 

    } 



func registerForKeyboardNotifications()-> Void { 

     NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWasShown(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil) 

     NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillBeHidden(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

    } 

func deregisterFromKeyboardNotifications() -> Void { 
     let center: NotificationCenter = NotificationCenter.default 
     center.removeObserver(self, name: NSNotification.Name.UIKeyboardDidShow, object: nil) 
     center.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 


    } 





func keyboardWasShown (_ notification: Notification) { 
     if(keyboardVisible == true){ 
      return 
     } 

     print(scrollView) 
     if let keyboardSize = ((notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { 

      offset = scrollView!.contentOffset 
      print(offset) 

      var viewFrame = scrollView!.frame 
      viewFrame.size.height -= keyboardSize.height 
      scrollView!.frame = viewFrame 

      var textFieldRect = activeField?.frame 
      textFieldRect?.origin.y += 10; 
      scrollView!.scrollRectToVisible(textFieldRect!, animated: true) 

      keyboardVisible = true; 


     } 


    } 

    func keyboardWillBeHidden (_ notification: Notification) { 

     if(keyboardVisible == false){ 
      return; 
     } 
     scrollView.frame = CGRect(x: 0, y: 0, width: scrollViewWidth, height: scrollViewHeight) 
     scrollView?.contentOffset = offset 

     keyboardVisible = false 


    } 


// textfield delegate 

//in textfieldDidBeginEditing i m setting custom toolbar on textfield input accesory view 
open func textFieldDidBeginEditing(_ textField: UITextField) { 
     if(textField == myTextField1){ 
      myTextField1.inputAccessoryView = myCustomToolbar 
      myCustomToolbar?.nextButton.isEnabled = true 
      myCustomToolbar?.previousButton.isEnabled = false 
      myCustomToolbar?.nextButton.title = "Next" 
     }else if(textField == myTextField2){ 
      myTextField2.inputAccessoryView = myCustomToolbar 
      myCustomToolbar?.previousButton.isEnabled = true 
      myCustomToolbar?.nextButton.isEnabled = true 
      myCustomToolbar?.nextButton.title = "Next" 
     }else if(textField == myTextField3){ 
      myTextField3.inputAccessoryView = myCustomToolbar 
      myCustomToolbar?.nextButton.isEnabled = true 
      myCustomToolbar?.previousButton.isEnabled = true 
      myCustomToolbar?.nextButton.title = "Next" 
     }else if(textField == myTextField4){ 
      myTextField4.inputAccessoryView = myCustomToolbar 
      myCustomToolbar?.nextButton.isEnabled = true 
      myCustomToolbar?.previousButton.isEnabled = true 
      myCustomToolbar?.changeNextToActionButton(withTitle: "Some title") 
     } 
    } 


    open func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 
     activeField = textField 
     return true 
    } 


//PnPToolbar Delegate 

    public func pnPToolbarNextButtonClicked(_ myCustomToolbar: myCustomToolbar!) { 
     if(myTextField1.isFirstResponder){ 
      myTextField2.becomeFirstResponder() 
     }else if(myTextField2.isFirstResponder){ 
      myTextField3.becomeFirstResponder() 
     }else if(myTextField3.isFirstResponder){ 
      myTextField4.becomeFirstResponder() 

     }else if(ifscCode.isFirstResponder){ 
      //Done call your method 
      self.myMethod() 
     } 
     var textFieldRect = activeField?.frame 
     textFieldRect?.origin.y += 10; 
     scrollView!.scrollRectToVisible(textFieldRect!, animated: true) 
    } 

    public func myCustomToolbarPreviousButtonClicked(_ myCustomToolbar: myCustomToolbar!) { 
     if(myTextField4.isFirstResponder){ 
      myTextField3.becomeFirstResponder() 
     }else if(myTextField3.isFirstResponder){ 
      myTextField2.becomeFirstResponder() 
     }else if(myTextField2.isFirstResponder){ 
      myTextField1.becomeFirstResponder() 
     } 
     var textFieldRect = activeField?.frame 
     textFieldRect?.origin.y += 10; 
     scrollView!.scrollRectToVisible(textFieldRect!, animated: true) 

    } 

當我從myTextField1移動到myTextField2光標談到第二文本框,但滾動型不動起來,滾動視圖的內容大小也無法超越鍵盤向上移動。

回答

0

管理您在TextField中的代表標誌如下:

func textFieldDidBeginEditing(_ textField: UITextField) { 
     keyboardVisible = true 
    } 

    func textFieldEndEditings(sender:UITextField) -> Void { 
     print(sender.text) 
     keyboardVisible = false 
    } 
0

工具欄鍵盤上方

let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50)) 
numberToolbar.barStyle = UIBarStyle.Default 
numberToolbar.items = [ 
      UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelNumberPad"), 
      UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil), 
      UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneWithNumberPad")] 
    numberToolbar.sizeToFit() 
    phonenumberTextField.inputAccessoryView = numberToolbar 
相關問題