Ios 编辑文本字段时,禁止用户与除键盘以外的UI交互

Ios 编辑文本字段时,禁止用户与除键盘以外的UI交互,ios,swift,keyboard,uitextfield,user-interaction,Ios,Swift,Keyboard,Uitextfield,User Interaction,斯威夫特的新手 当用户单击文本字段开始键入时,除了在该文本字段中键入外,是否有其他方法禁用所有用户交互?我希望这样,用户就无法在文本字段外单击,并且应该按回车键关闭键盘,此时再次启用用户交互(我已设置了“关闭键盘”部分) 我知道isUserInteractionEnabled函数可以停止所有交互,但我仍然希望用户能够与键盘交互 谢谢您描述的行为可以通过操纵布尔值isEnabled值if UI元素来实现。有两个步骤可以使这个过程变得平滑和用户友好 这只是一个很好的实践,本例不需要,但我喜欢在其中扩

斯威夫特的新手

当用户单击文本字段开始键入时,除了在该文本字段中键入外,是否有其他方法禁用所有用户交互?我希望这样,用户就无法在文本字段外单击,并且应该按回车键关闭键盘,此时再次启用用户交互(我已设置了“关闭键盘”部分)

我知道isUserInteractionEnabled函数可以停止所有交互,但我仍然希望用户能够与键盘交互


谢谢

您描述的行为可以通过操纵布尔值
isEnabled
值if 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.
    }
}
<代码>导入基础 导入UIKit 类MyViewController:UIViewController、UIExtFieldDelegate{ @IBVar someButton:UIButton! @IBOutlet弱var topTextField:UITextField! @ibextfield:UITextField! 重写func viewDidLoad(){ super.viewDidLoad() //将代理设置为self,在本例中不显示。 topTextField.delegate=self bottomTextField.delegate=self enableUIElements(真) } //委托函数在文本字段中键入内容。 func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{ enableUIElements(假) var newText=textField.text!作为NSString newText=newText.replacingCharacters(在:范围内,带:字符串)作为NSString 让textSize:CGSize=newText.size(属性:[NSFontAttributeName:textField.font!) 返回textSize.widthBool{ textField.resignFirstResponder()//关闭键盘。 enableUIElements(true)//启用UI元素。 返回真值 } //创建一个函数来启用/禁用UI元素。 func enableUIElements(uu启用:Bool){ someButton.isEnabled=启用 //TODO:在此处添加其他元素。 } } 注意最后一个函数!!!在键盘处于活动状态时使用它来启用/禁用其他元素,如我所示


这应该能让你找到你需要去的地方:)

因此,我想到的第一个想法是,在你关注UITextField实例时,禁用视图中的每个UI元素

创建一个函数,该函数将获取一个UI元素作为参数(ex的UITextField)。在函数体中,开始检查每个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
    }
} 

您可以对每个UITextField的
onDidEnd
iAction执行此方法。如果要运行正常的应用程序行为,最后一步是必需的。

不应该是自动的,您需要这样编码,以编程方式禁用所有其他功能。对。这就是我想知道怎么做的。我知道userInteractionEnabled可以停止所有交互,但我仍然希望用户能够与键盘交互例如,对于UI按钮,您应该执行您的Buttonname.isEnabled=falseright这是按钮,但我想禁用用户交互,除了与keyboard@brownmamba既然你还没有接受答案,我相信你还在寻找答案。我没有一个编码的解决方案,但为什么不在键盘出来时用另一个视图屏蔽整个视图呢?这还允许您为遮罩视图指定颜色(假设不透明度为0.4),这将帮助用户理解为什么在键盘区域外单击时不会发生任何事情。您可以使用UIKeyboardWillShowNotification和UIKeyboardWillHideNotification来计算大小,并显示/隐藏遮罩视图。这与我尝试实现的目标非常接近。我正在使用表视图单元格,并且有一个textField元素来控制所有单元格。因此,在您的回答下,用户可以按下另一个单元格的文本字段,从而启用交互。有没有办法只关注一个单元格的文本字段?所以他们甚至不能点击其他文本字段?