2017-01-09 24 views
1

Swift的新功能。在編輯文本字段時禁止用戶與用戶界面進行除鍵盤以外的交互

當用戶單擊文本字段開始輸入時,是否有辦法禁用除鍵入該文本字段之外的所有用戶交互?我需要它,以便用戶無法在文本字段外點擊,並且應該按下返回鍵以關閉鍵盤,在此處再次啓用用戶交互(我設置瞭解除鍵盤部分)。

我知道isUserInteractionEnabled函數可以停止所有的交互,但我仍然希望用戶能夠與鍵盤交互。

感謝

+0

不應該是自動的,你需要像這樣編寫代碼,以編程方式禁用所有其他功能。 –

+0

沒錯。這就是我想知道如何去做的。我知道userInteractionEnabled可以停止所有交互,但我仍然希望用戶能夠與鍵盤進行交互 – brownmamba

+0

例如,對於UI按鈕,您應該執行yourButtonName的 。isEnabled = false –

回答

0

您所描述的行爲可以通過操縱布爾isEnabled值,如果UI元素來完成。有幾個步驟可以使這個平滑和用戶友好。

剛剛好做法,並沒有要求這個例子,但我喜歡做MyViewController的擴展,訂閱/退訂鍵盤通知等在裏面,像這樣:

import Foundation 
import UIKit 

extension MyViewController {    

    //MARK: Subscribing/Unsubribing to NotificationCenter. 
    func subcribeToKeyboardNotifications(){  
     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 unsubscribeToKeyboardNotifications(){ 
     NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    } 

    //MARK: Screen manipulation methods, to move the UIView up when the bottom text field is editing. 
    func keyboardWillShow(_ notification: Notification) { 
     if bottomTextField.isEditing { 
      view.frame.origin.y = getKeyboardHeight(notification) * -1 
     } 
    } 

    func keyboardWillHide(_ notification: Notification) { 
     if bottomTextField.isEditing { 
      view.frame.origin.y = 0 
     } 
    } 

    //MARK: Value returning method to calculate keyboard height. 
    private func getKeyboardHeight(_ notification: Notification) -> CGFloat { 
     let userInfo = (notification as NSNotification).userInfo! 
     let keyboardSize = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue 
     return keyboardSize.cgRectValue.height 
    } 
} 

接下來,在MyViewController中,當返回鍵被點擊時,我將鍵盤設置爲resignToFirstResponder(),並且我確保設置我的UITextFieldDelegates

import Foundation 
import UIKit 

class MyViewController: UIViewController, UITextFieldDelegate { 
    @IBOutlet weak var someButton: UIButton! 
    @IBOutlet weak var topTextField: UITextField! 
    @IBOutlet weak var bottomTextField: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     //Set the delegates to self, it this example. 
     topTextField.delegate = self 
     bottomTextField.delegate = self 
     enableUIElements(true) 
    } 

    //Delegate function to type stuff into the text field. 
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
     enableUIElements(false) 
     var newText = textField.text! as NSString 
     newText = newText.replacingCharacters(in: range, with: string) as NSString 
     let textSize: CGSize = newText.size(attributes: [NSFontAttributeName: textField.font!]) 
     return textSize.width < textField.bounds.size.width 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() //Dismiss the keyboard. 
     enableUIElements(true) //enable the UI elements. 
     return true 
    } 

    //Create a function to enable/disable UI elements. 
    func enableUIElements(_ enable: Bool){ 
     someButton.isEnabled = enable 
     //TODO: add other elements here. 
    } 
} 

請注意,最後的功能!正如我所展示的,使用它來啓用/禁用鍵盤處於活動狀態時的其他元素。

,你需要去:)

1

所以,第一個想法,它向我走來時禁用每個UI元素到你的觀點,當你專注於的UITextField實例應該得到你。

創建一個函數,它將獲得一個UI元素作爲參數(UITextField for ex)。在函數體中開始檢查每個UI元素並週期性地禁用每個人,這不等於傳遞的參數。 您可以另外驗證您的文本字段的類型和文字。

這裏的函數體的草案:

func disableAll(exceptInput element: UITextField) { 
    for item in self.view.subviews { 
     if item != element { 
      item.isUserInteractionEnabled = false 
     } else { 
      item.isUserInteractionEnabled = true 
     } 
    } 
} 

,並在文本領域採取行動執行這種方法,爲前。在onDidStart

而且,顯然,不要忘記爲用戶交互啓用所有元素。代碼很簡單:

func enableAll() { 
    for item in self.view.subviews { 
     item.isUserInteractionEnabled = true 
    } 
} 

您可以在onDidEnd IBAction爲每一個的UITextField執行這種方法。如果你想運行正常的應用程序行爲,最後一步是必需的。

+0

這非常接近我想要實現的目標。我正在使用表格視圖單元格,並有一個textField元素來控制所有單元格。因此,用您的答案,用戶可以按下另一個單元格的textField,並啓用交互。有沒有辦法專注於一個單元格的textField?所以他們甚至不能點擊其他的textFields? – brownmamba

相關問題