Ios 如何在Swift中切换UITextField安全文本输入(隐藏密码)?
我现在有一个Ios 如何在Swift中切换UITextField安全文本输入(隐藏密码)?,ios,swift,uitextfield,Ios,Swift,Uitextfield,我现在有一个UITextfield,里面有一个眼睛图标,按下这个图标可以打开和关闭安全文本条目 我知道您可以选中属性检查器中的“安全文本输入”框,但如何做到这一点,以便每当按下图标时它都会切换?试试这一行: @IBAction func btnClick(sender: AnyObject) { let btn : UIButton = sender as! UIButton if btn.tag == 0{ btn.tag = 1 textFi
UITextfield
,里面有一个眼睛图标,按下这个图标可以打开和关闭安全文本条目
我知道您可以选中属性检查器中的“安全文本输入”框,但如何做到这一点,以便每当按下图标时它都会切换?试试这一行:
@IBAction func btnClick(sender: AnyObject) {
let btn : UIButton = sender as! UIButton
if btn.tag == 0{
btn.tag = 1
textFieldSecure.secureTextEntry = NO
}
else{
btn.tag = 0
textFieldSecure.secureTextEntry = NO;
}
}
使用带有眼睛图像的按钮
并制作按钮手柄方法
为值为1的按钮设置标记
-(IBAction) buttonHandlerSecureText:(UIButton *)sender{
if(sender.tag ==1){
[self.textField setSecureTextEntry:NO];
sender.tag = 2;
}
else{
[self.textField setSecureTextEntry:YES];
sender.tag = 1;
}
}
赋值值从
YES/NO
更改为true/false
布尔值
password.secureTextEntry = true //Visible
password.secureTextEntry = false //InVisible
你可以试试这个代码。。
我认为这很有帮助。使用此代码
iconClick是bool变量,或者您需要其他条件检查它
var iconClick = true
眼睛动作方法:
@IBAction func iconAction(sender: AnyObject) {
if(iconClick == true) {
passwordTF.secureTextEntry = false
} else {
passwordTF.secureTextEntry = true
}
iconClick = !iconClick
}
希望它有帮助正如其他人所指出的,该属性是
secureTextEntry
,但您在UITextField
文档中找不到它,因为它实际上是由UITextField
通过UITEXTRAITS
协议继承的
您只需在每次点击按钮时切换此值:
@IBAction func togglePasswordSecurity(sender: UIButton) {
self.passwordField.secureTextEntry = !self.passwordField.secureTextEntry
}
以下是您的答案,无需采取任何布尔值:
@IBAction func showHideAction(sender: AnyObject) {
if tfPassword.secureTextEntry{
tfPassword.secureTextEntry = false
}else{
tfPassword.secureTextEntry = true;
}
}
为什么要使用额外的
var
。在眼睛按钮的动作方法中,只需执行以下操作
password.secureTextEntry = !password.secureTextEntry
更新
Swift 4.2(根据@ROC评论)
针对目标c 在viewdidload方法中为RightButton设置图像
[RightButton setImage:[UIImage imageNamed:@"iconEyesOpen"] forState:UIControlStateNormal];
[RightButton setImage:[UIImage imageNamed:@"iconEyesClose"] forState:UIControlStateSelected];
然后设置右按钮的操作方法
-(IBAction)RightButton:(id)sender
{
if (_rightButton.selected)
{
_rightButton.selected = NO;
_passwordText.secureTextEntry = YES;
if (_passwordText.isFirstResponder) {
[_passwordText resignFirstResponder];
[_passwordText becomeFirstResponder];
}
}
else
{
_rightButton.selected = YES;
_passwordText.secureTextEntry = NO;
if (_passwordText.isFirstResponder) {
[_passwordText resignFirstResponder];
[_passwordText becomeFirstResponder];
}
}
}
使用UITextField rightView显示切换按钮
var rightButton = UIButton(type: .custom)
rightButton.frame = CGRect(x:0, y:0, width:30, height:30)
yourtextfield.rightViewMode = .always
yourtextfield.rightView = rightButton
Swift 3
passwordTF.isSecureTextEntry = true
passwordTF.isSecureTextEntry = false
// MARK: Btn EyeAction
@IBAction func btnEyeAction(_ sender: Any) {
if(iconClick == true) {
txtPassword.isSecureTextEntry = false
iconClick = false
} else {
txtPassword.isSecureTextEntry = true
iconClick = true
}
}
Swift 3
passwordTF.isSecureTextEntry = true
passwordTF.isSecureTextEntry = false
// MARK: Btn EyeAction
@IBAction func btnEyeAction(_ sender: Any) {
if(iconClick == true) {
txtPassword.isSecureTextEntry = false
iconClick = false
} else {
txtPassword.isSecureTextEntry = true
iconClick = true
}
}
首先,您需要为不同的状态(选定或正常)设置眼睛按钮的图像(可见或隐藏) 然后连接iAction并编写如下代码
@IBAction func btnPasswordVisiblityClicked(_ sender: Any) {
(sender as! UIButton).isSelected = !(sender as! UIButton).isSelected
if (sender as! UIButton).isSelected {
txtfPassword.isSecureTextEntry = false
} else {
txtfPassword.isSecureTextEntry = true
}
}
对于Xamarin人:
passwordField.SecureTextEntry=passwordField.SecureTextEntry?passwordField.SecureTextEntry=false:passwordField.SecureTextEntry=true 这样做的一个意外副作用是,如果用户切换到“不安全”,然后再切换回“安全”,则如果用户继续键入,现有文本将被清除。除非我们重置选定的文本范围,否则光标也可能最终位于错误的位置 下面是处理这些情况的实现(Swift 4)
此代码段正在使用
replace(:withText:)
函数,因为它触发了.editingChanged
事件,该事件恰好在我的应用程序中很有用。只需设置text=existingText
也可以。在swift 4中尝试此代码,尝试在控制器中生成可重用代码。我在故事板中为按钮设置了不同的图像,如链接所示
Swift 4解决方案 对于简单切换ISSecureTextertry属性,不需要额外的if语句
func togglePasswordVisibility() {
password.isSecureTextEntry = !password.isSecureTextEntry
}
但是当您切换isSecureTextEntry UITextField时会出现问题,因为它不会重新计算文本宽度,并且文本右侧有额外的空间。为了避免这种情况,您应该以这种方式替换文本
func togglePasswordVisibility() {
password.isSecureTextEntry = !password.isSecureTextEntry
if let textRange = password.textRange(from: password.beginningOfDocument, to: password.endOfDocument) {
password.replace(textRange, withText: password.text!)
}
}
更新
Swift 4.2
而不是
password.isSecureTextEntry = !password.isSecureTextEntry
你可以这样做
password.isSecureTextEntry.toggle()
在Swift 4中
var iconClick : Bool!
override func viewDidLoad() {
super.viewDidLoad()
iconClick = true
}
@IBAction func showHideAction(_ sender: Any)
{
let userPassword = userPasswordTextFiled.text!;
if(iconClick == true) {
userPasswordTextFiled.isSecureTextEntry = false
iconClick = false
} else {
userPasswordTextFiled.isSecureTextEntry = true
iconClick = true
}
}
如果您需要在多个位置具有类似功能的TextField,最好将
UITextField
子类化,如下面的示例-
import UIKit
class UIShowHideTextField: UITextField {
let rightButton = UIButton(type: .custom)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
required override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
rightButton.setImage(UIImage(named: "password_show") , for: .normal)
rightButton.addTarget(self, action: #selector(toggleShowHide), for: .touchUpInside)
rightButton.frame = CGRect(x:0, y:0, width:30, height:30)
rightViewMode = .always
rightView = rightButton
isSecureTextEntry = true
}
@objc
func toggleShowHide(button: UIButton) {
toggle()
}
func toggle() {
isSecureTextEntry = !isSecureTextEntry
if isSecureTextEntry {
rightButton.setImage(UIImage(named: "password_show") , for: .normal)
} else {
rightButton.setImage(UIImage(named: "password_hide") , for: .normal)
}
}
}
之后,您可以在任何ViewController中使用它
class ViewController: UIViewController {
@IBOutlet var textField: UIShowHideTextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.becomeFirstResponder()
}
}
下面是一个使用Switch语句的简单易读的解决方案。Shortest!
我认为这是安全进入和更新按钮图片的最短解决方案
@IBAction func toggleSecureEntry(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
textfieldPassword.isSecureTextEntry = !sender.isSelected
}
根据选中/默认状态分配按钮的显示/隐藏图片,无需创建任何变量或出口。希望这是一个更简单的解决方案,而不是全局创建布尔对象
@IBAction func passwordToggleButton(sender: UIButton) {
let isSecureTextEntry = passwordTextField.isSecureTextEntry
passwordTextField.isSecureTextEntry = isSecureTextEntry ? false : true
if isSecureTextEntry {
visibilityButton.setImage(UIImage(named: "visibility"), for: .normal)
} else {
visibilityButton.setImage(UIImage(named: "visibility_off"), for: .normal)
}
}
我写了同样的扩展。提供密码切换
extension UITextField {
fileprivate func setPasswordToggleImage(_ button: UIButton) {
if(isSecureTextEntry){
button.setImage(UIImage(named: "ic_password_visible"), for: .normal)
}else{
button.setImage(UIImage(named: "ic_password_invisible"), for: .normal)
}
}
func enablePasswordToggle(){
let button = UIButton(type: .custom)
setPasswordToggleImage(button)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.frame = CGRect(x: CGFloat(self.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.addTarget(self, action: #selector(self.togglePasswordView), for: .touchUpInside)
self.rightView = button
self.rightViewMode = .always
}
@IBAction func togglePasswordView(_ sender: Any) {
self.isSecureTextEntry = !self.isSecureTextEntry
setPasswordToggleImage(sender as! UIButton)
}
}
override func viewDidLoad() {
super.viewDidLoad()
txtPassword.enablePasswordToggle()
txtConfirmPassword.enablePasswordToggle()
}
仅将这一行添加到您的代码中,用“TextField”替换您的TextField name完成: 您需要更改IsSecurity Extentry属性,以便将密码类型textfield更改为true,如
textField.isSecureTextEntry = true
请编辑更多信息。不鼓励只编写代码和“试试这个”答案,因为它们不包含可搜索的内容,也不解释为什么有人应该“试试这个”。我们在这里努力成为知识资源。当secureTextEntry设置为false时,这不会将密码泄漏到自定义键盘中吗?只是一个快速更新。secureTextEntry现在是。isSecureTextEntry@Iyyappan拉维,如果我有多个密码文本字段来实现相同的。我是否需要为每个密码文本字段添加单独的操作?如何使其通用?@Prabu将动作从多个按钮集标记设置为单独的方法,并像上面解决的方法一样,希望它有帮助。回答得很好!!谢谢你!非常感谢。但这并不能回答问题,也不能解决问题。这里不需要额外的
var
,我认为@Suryakant发布的答案是最好的。在swift 4.2中,你可以编写password.issecureExtentry.toggle()
太好了,这就是我想要的!您还可以执行text=nil;insertText(originalText)代替deleteBackward()技巧,该技巧可以在最新的iOS版本上立即显示文本选择。您还可以在函数开头让originalSelectedTextRange=selectedTextRange保持原始位置,如果光标不在文本的末尾,我在这里和那里尝试了许多解决方案,直到我找到了这个。其他每一个解决方案都至少引入了一个新问题。在我看来,这是一个完美的解决方案,没有任何副作用。我有一个问题,当我显示和隐藏密码时,它就像一个高亮动画。知道吗?在使用xcode11的swift 5中,我无法在uibuttont上渲染图像
extension UITextField {
fileprivate func setPasswordToggleImage(_ button: UIButton) {
if(isSecureTextEntry){
button.setImage(UIImage(named: "ic_password_visible"), for: .normal)
}else{
button.setImage(UIImage(named: "ic_password_invisible"), for: .normal)
}
}
func enablePasswordToggle(){
let button = UIButton(type: .custom)
setPasswordToggleImage(button)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.frame = CGRect(x: CGFloat(self.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.addTarget(self, action: #selector(self.togglePasswordView), for: .touchUpInside)
self.rightView = button
self.rightViewMode = .always
}
@IBAction func togglePasswordView(_ sender: Any) {
self.isSecureTextEntry = !self.isSecureTextEntry
setPasswordToggleImage(sender as! UIButton)
}
}
override func viewDidLoad() {
super.viewDidLoad()
txtPassword.enablePasswordToggle()
txtConfirmPassword.enablePasswordToggle()
}
textField.isSecureTextEntry = true
sender.isSelected = !sender.isSelected
if(sender.isSelected == true) {
RegPasswordField.isSecureTextEntry = false
sender.setBackgroundImage(UIImage(systemName: "eye.fill"), for: .normal)
} else {
RegPasswordField.isSecureTextEntry = true
sender.setBackgroundImage(UIImage(systemName: "eye"), for: .normal)
}