kis*_*011 5 uitextfield ios swift
我正在尝试切换密码文本字段,但我面临文本字段右侧的空白问题.
//眼睛按钮动作
@IBAction func btnEyePassword(sender: AnyObject)
{
//If password text is secure.
if (self.passrordText.secureTextEntry == true)
{
self.passrordText.secureTextEntry = false
}
else
{
self.passrordText.secureTextEntry = true
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 4解决方案
func togglePasswordVisibility() {
password.isSecureTextEntry = !password.isSecureTextEntry
if let textRange = password.textRange(from: password.beginningOfDocument, to: password.endOfDocument) {
password.replace(textRange, withText: password.text!)
}
}
Run Code Online (Sandbox Code Playgroud)
对于简单的切换isSecureTextEntry属性,您不需要额外的if语句,但是您应该以这种方式替换文本,以强制UITextField重新计算文本宽度,以避免多余的空间。
更新
斯威夫特4.2
代替
password.isSecureTextEntry = !password.isSecureTextEntry
Run Code Online (Sandbox Code Playgroud)
你可以这样做
password.isSecureTextEntry.toggle()
Run Code Online (Sandbox Code Playgroud)