2016-01-01 21 views
4

我試圖設計我的應用程序時,UITextField內的文本不符合特定正則表達式顯示錯誤UILabel內的視圖。除了我的UITextField包含無效條目,然後點擊屏幕以關閉鍵盤時,一切正常。鍵盤駁回後,我UITextView(紅色文本)的頂部UITextField移動,我無法找出原因。我在代碼中設置了斷點,但似乎沒有任何代碼在鍵盤解散後執行。任何幫助表示讚賞。誤差的UITextField移動

Image of UITextField covering up UITextView

// 
// ViewController.swift 
// KeyboardLabel 
// 

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet var titleLabel:UILabel! 
    @IBOutlet var errorLabel:UITextView! 
    @IBOutlet var textBox:UITextField! 
    @IBOutlet var createButton:UIButton! 

    var deltaHeight:CGFloat! 
    var beenMoved = true 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Register for notifications when the text in the text field is changed 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "validateText", name: UITextFieldTextDidChangeNotification, object: textBox) 

     // Add a gesture recognizer to dismiss the keyboard 
     view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "dismissKeyboard")) 

     // Set the text of the labels 
     titleLabel.text = "Container Number:" 
     errorLabel.text = "Error, Invalid container number.\nRe-enter number." 

     // Calculate the height to move the text field and button 
     deltaHeight = errorLabel.frame.size.height + 8.0 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func dismissKeyboard() { 
     textBox.resignFirstResponder() 
    } 

    func validateText() { 
     dispatch_async(dispatch_get_main_queue(), { 
      // Regular expression for determining whether the text is valid 
      let predicate = NSPredicate(format: "SELF MATCHES %@", "[a-zA-Z0-9#]+") 
      let empty = self.textBox.text!.isEmpty 
      let valid = predicate.evaluateWithObject(self.textBox.text) 

      // If the string is valid 
      if valid || empty { 
       // If the view has been moved down 
       if(!self.beenMoved) { 
        // Hide the error label then move the text field and create button up 
        self.errorLabel.hidden = true 
        UIView.animateWithDuration(0.4, animations: { 
         self.textBox.frame.origin.y -= self.deltaHeight 
         self.createButton.frame.origin.y -= self.deltaHeight 
        }) 

        // Flip the flag 
        self.beenMoved = true 
       } 
      } 
       // If the string is invalid 
      else { 
       // If the view has been moved up 
       if(self.beenMoved) { 
        // Show the error label then move the text field and create button down 
        UIView.animateWithDuration(0.4, animations: { 
         self.textBox.frame.origin.y += self.deltaHeight 
         self.createButton.frame.origin.y += self.deltaHeight 
         }, completion: { 
          (flag:Bool) in 
          self.errorLabel.hidden = false 
        }) 

        // Flip the flag 
        self.beenMoved = false 
       } 
      } 

      // If the text field is empty 
      if empty { 
       // Disable the create button 
       self.createButton.enabled = false 
      } 
      else { 
       // Enable the create button 
       self.createButton.enabled = true 
      } 
     }) 
    } 
} 
+1

我懷疑你做了,因爲自動佈局的麻煩。使用自動佈局時,您不應該更改視圖的框架。您應該爲定位視圖的約束創建'@ IBOutlet',並更新代碼中的'constant'屬性以重新定位視圖。 – vacawama

+0

@vacawama謝謝。我通過創建'@ IBOutlet's控制在'errorLabel'的高度,並在'textBox'頂部空間的約束來解決這個問題。 – dice

回答

0

行爲實際上是這樣的,最初的的UITextField的UITextView的頂部,如果你輸入的東西,不,正則表達式匹配移動。這是因爲你用下面的代碼行移動的UITextField和按鈕:

self.textBox.frame.origin.y += self.deltaHeight 
    self.createButton.frame.origin.y += self.deltaHeight 

當鍵盤駁回,給出UITextField和UIButton的恢復到原來的位置。我假設你對UI元素沒有限制。