在swift中禁用UITextfield的用户输入

时间:2022-11-29 17:24:52

pretty trivial question, I know. But I can not find anything online.

我知道这个问题很琐碎。但是我在网上找不到任何东西。

I need to disable the user from being able to edit the text inside of a text field. So that when the click on the text, a keyboard doesn't show up.

我需要禁止用户编辑文本字段中的文本。所以当点击文本时,键盘不会出现。

Any ideas?

什么好主意吗?

A programmatic solution or if it is possible through storyboards would be great.

一个程序化的解决方案或者如果可以通过故事板的话,那将是非常好的。

5 个解决方案

#1


68  

Try this:

试试这个:

Swift 2.0:

斯威夫特2.0:

textField.userInteractionEnabled = false

Swift 3.0:

斯威夫特3.0:

textField.isUserInteractionEnabled = false

Or in storyboard uncheck "User Interaction Enabled"

或者在故事板中取消"用户交互启用"

在swift中禁用UITextfield的用户输入

#2


21  

Another solution, declare your controller as UITextFieldDelegate, implement this call-back:

另一个解决方案,将您的控制器声明为UITextFieldDelegate,实现这个回调:

@IBOutlet weak var myTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    myTextField.delegate = self
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    if textField == myTextField {
        return false; //do not show keyboard nor cursor
    }
    return true
}

#3


16  

In storyboard you have two choise:

在故事板中你有两个选择:

  1. set the control's 'enable' to false.
  2. 将控件的“enable”设置为false。

在swift中禁用UITextfield的用户输入

  1. set the view's 'user interaction enable' false
  2. 设置视图的“用户交互启用”为false

在swift中禁用UITextfield的用户输入

The diffierence between these choise is:

这些选择之间的差别是:

the appearance of UITextfild to display in the screen.

UITextfild的外观显示在屏幕上。

在swift中禁用UITextfield的用户输入

  1. First is set the control's enable. You can see the backgroud color is changed.
  2. 首先设置控件的enable。你可以看到背景颜色改变了。
  3. Second is set the view's 'User interaction enable'. The backgroud color is NOT changed.
  4. 第二是设置视图的“用户交互启用”。背景颜色没有改变。

Within code:

在代码:

  1. textfield.enable = false
  2. 文本框。使= false
  3. textfield.userInteractionEnabled = NO

    文本框。userInteractionEnabled =没有

    Updated for Swift 3

    更新快3

    1. textField.isEnabled = false
    2. 文本框。isEnabled = false
    3. textfield.isUserInteractionEnabled = false
    4. 文本框。isUserInteractionEnabled = false

#4


2  

I like to do it like old times. You just use a custom UITextField Class like this one:

我喜欢像以前那样做。你只需要使用自定义UITextField类,比如这个:

//
//  ReadOnlyTextField.swift
//  MediFormulas
//
//  Created by Oscar Rodriguez on 6/21/17.
//  Copyright © 2017 Nica Code. All rights reserved.
//

import UIKit

class ReadOnlyTextField: UITextField {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    override init(frame: CGRect) {
        super.init(frame: frame)

        // Avoid keyboard to show up
        self.inputView = UIView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Avoid keyboard to show up
        self.inputView = UIView()
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // Avoid cut and paste option show up
        if (action == #selector(self.cut(_:))) {
            return false
        } else if (action == #selector(self.paste(_:))) {
            return false
        }

        return super.canPerformAction(action, withSender: sender)
    }

}

#5


0  

you can use UILabel instead if you don't want the user to be able to modify anything in your UITextField

如果不希望用户能够修改UITextField中的任何内容,可以使用UILabel

A programmatic solution would be to use enabled property:

一个方案解决办法是使用启用的属性:

yourTextField.enabled = false

A way to do it in a storyboard:

在故事板中做这个的方法是

Uncheck the Enabled checkbox in the properties of your UITextField

在UITextField的属性中取消选中的复选框

在swift中禁用UITextfield的用户输入

#1


68  

Try this:

试试这个:

Swift 2.0:

斯威夫特2.0:

textField.userInteractionEnabled = false

Swift 3.0:

斯威夫特3.0:

textField.isUserInteractionEnabled = false

Or in storyboard uncheck "User Interaction Enabled"

或者在故事板中取消"用户交互启用"

在swift中禁用UITextfield的用户输入

#2


21  

Another solution, declare your controller as UITextFieldDelegate, implement this call-back:

另一个解决方案,将您的控制器声明为UITextFieldDelegate,实现这个回调:

@IBOutlet weak var myTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    myTextField.delegate = self
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    if textField == myTextField {
        return false; //do not show keyboard nor cursor
    }
    return true
}

#3


16  

In storyboard you have two choise:

在故事板中你有两个选择:

  1. set the control's 'enable' to false.
  2. 将控件的“enable”设置为false。

在swift中禁用UITextfield的用户输入

  1. set the view's 'user interaction enable' false
  2. 设置视图的“用户交互启用”为false

在swift中禁用UITextfield的用户输入

The diffierence between these choise is:

这些选择之间的差别是:

the appearance of UITextfild to display in the screen.

UITextfild的外观显示在屏幕上。

在swift中禁用UITextfield的用户输入

  1. First is set the control's enable. You can see the backgroud color is changed.
  2. 首先设置控件的enable。你可以看到背景颜色改变了。
  3. Second is set the view's 'User interaction enable'. The backgroud color is NOT changed.
  4. 第二是设置视图的“用户交互启用”。背景颜色没有改变。

Within code:

在代码:

  1. textfield.enable = false
  2. 文本框。使= false
  3. textfield.userInteractionEnabled = NO

    文本框。userInteractionEnabled =没有

    Updated for Swift 3

    更新快3

    1. textField.isEnabled = false
    2. 文本框。isEnabled = false
    3. textfield.isUserInteractionEnabled = false
    4. 文本框。isUserInteractionEnabled = false

#4


2  

I like to do it like old times. You just use a custom UITextField Class like this one:

我喜欢像以前那样做。你只需要使用自定义UITextField类,比如这个:

//
//  ReadOnlyTextField.swift
//  MediFormulas
//
//  Created by Oscar Rodriguez on 6/21/17.
//  Copyright © 2017 Nica Code. All rights reserved.
//

import UIKit

class ReadOnlyTextField: UITextField {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    override init(frame: CGRect) {
        super.init(frame: frame)

        // Avoid keyboard to show up
        self.inputView = UIView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Avoid keyboard to show up
        self.inputView = UIView()
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // Avoid cut and paste option show up
        if (action == #selector(self.cut(_:))) {
            return false
        } else if (action == #selector(self.paste(_:))) {
            return false
        }

        return super.canPerformAction(action, withSender: sender)
    }

}

#5


0  

you can use UILabel instead if you don't want the user to be able to modify anything in your UITextField

如果不希望用户能够修改UITextField中的任何内容,可以使用UILabel

A programmatic solution would be to use enabled property:

一个方案解决办法是使用启用的属性:

yourTextField.enabled = false

A way to do it in a storyboard:

在故事板中做这个的方法是

Uncheck the Enabled checkbox in the properties of your UITextField

在UITextField的属性中取消选中的复选框

在swift中禁用UITextfield的用户输入