2016-09-15 110 views
4

如何從UITextField禁用Swift中的光標?條件 - 當用戶登錄時,然後進入應用程序。如果用戶可以從應用程序註銷,則不會將光標顯示到UITextField中。在swift中禁用UITextField中的閃爍光標?

+1

簡單的改變當前的文本框爲'tintColor =的UIColor clearColor]' –

+0

你確認UITextFieldDelegate的色調顏色? –

回答

1

首先,你需要確認到UITextFieldDelegate然後設置文本框狀yourTextField.delegate =self 添加 yourTextField.resignFirstResponder()

你的代碼。將resignFirstResponder調用到yourTextField。 ,你也可以使用下面的代碼(根據你的需要)。

在委託方法textFieldDidEndEditing(textField: UITextField)與檢查(如果)它是否有一些值/文本不是。

yourTextField.attributedPlaceholder = NSAttributedString(string: "User Name", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()]) textUserName.resignFirstResponder()

4

最初在user default,當用戶按下退出鍵設置的值作爲NO

爲如

UserDefaults.standard().set(true, forKey: "disalert") 

當過你來到這個頁面檢查等作爲

條件設置一些布爾值
let disalert: Bool = UserDefaults.standard().bool(forKey: "disalert") 

     self.youtextField().tintColor = UIColor.blueColor() 
    if disalert { 
     self.youtextField().tintColor = UIColor.clearColor() 
     self.youtextField().resignFirstresponder() 
    } 

當用戶按下登錄按鈕時將用戶默認值設置爲

UserDefaults.standard().set(false, forKey: "disalert") 

使用像

(self.youtextField.valueForKey("textInputTraits") as! String)["insertionPointColor"] = UIColor.clearColor() 

或設置像

self.youtextField().tintColor = UIColor.clearColor() 
+0

self.youtextField()。tintColor = UIColor.clearColor()爲我工作 – Infaz

6

,如果你只是不想cursor那麼你可以設置它的tintColorclearColor。在這種情況下,你的textField將被激活。如果你想使它成爲非活動領域,那麼你應該打resignFirstresponder方法textfield