ios 快速禁用 UITextfield 的用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29791644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling user input for UITextfield in swift
提问by Matt Spoon
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.
一个程序化的解决方案或者如果可以通过故事板会很棒。
回答by Vlad
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"
或者在情节提要中取消选中“启用用户交互”
回答by Duyen-Hoa
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
}
回答by dusty
In storyboard you have two choise:
在故事板中,您有两种选择:
- set the control's 'enable' to false.
- 将控件的“启用”设置为 false。
- set the view's 'user interaction enable' false
- 将视图的“用户交互启用”设置为 false
The diffierence between these choise is:
这些选择之间的区别是:
the appearance of UITextfild to display in the screen.
要在屏幕上显示的 UITextfild 的外观。
- First is set the control's enable. You can see the backgroud color is changed.
- Second is set the view's 'User interaction enable'. The backgroud color is NOT changed.
- 首先是设置控件的启用。您可以看到背景颜色已更改。
- 其次是设置视图的“用户交互启用”。背景颜色没有改变。
Within code:
在代码中:
textfield.enable = false
textfield.userInteractionEnabled = NO
Updated for Swift 3
textField.isEnabled = false
textfield.isUserInteractionEnabled = false
textfield.enable = false
textfield.userInteractionEnabled = NO
为 Swift 3 更新
textField.isEnabled = false
textfield.isUserInteractionEnabled = false
回答by mld.oscar
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)
}
}
回答by jeet.chanchawat
If you want to do it while keeping the user interaction on.
In my case I am using (or rather misusing) isFocused
如果您想在保持用户交互的同时执行此操作。就我而言,我正在使用(或者更确切地说是滥用)isFocused
self.myField.inputView = UIView()
This way it will focus but keyboard won't show up.
这样它会聚焦但键盘不会出现。
回答by Novarg
you can use UILabel
instead if you don't want the user to be able to modify anything in your UITextField
UILabel
如果您不希望用户能够修改您的任何内容,则可以改用UITextField
A programmatic solution would be to use enabled
property:
一个程序化的解决方案是使用enabled
属性:
yourTextField.enabled = false
A way to do it in a storyboard:
在故事板中执行此操作的方法:
Uncheck the Enabledcheckbox in the properties of your UITextField
取消选中您的属性中的启用复选框UITextField
回答by FRIDDAY
Swift 4.2 / Xcode 10.1:
斯威夫特 4.2 / Xcode 10.1:
Just uncheck behavior Enabledin your storyboard -> attributes inspector.
只需取消选中在故事板中启用的行为-> 属性检查器。