Ios UITextField secureEntry错误放置光标

Ios UITextField secureEntry错误放置光标,ios,uitextfield,Ios,Uitextfield,我在切换UITextField上的secureEntry属性时遇到问题。切换属性时,字符的大小会调整,但光标会停留在错误的位置: 以下是我的解决方法: textField.secureTextEntry = YES; UITextRange *textRange = textField.selectedTextRange; textField.selectedTextRange = nil; textField.selectedTextRange = textRange; 禁用然后启用UI

我在切换
UITextField
上的
secureEntry
属性时遇到问题。切换属性时,字符的大小会调整,但光标会停留在错误的位置:

以下是我的解决方法:

textField.secureTextEntry = YES;

UITextRange *textRange = textField.selectedTextRange;
textField.selectedTextRange = nil;
textField.selectedTextRange = textRange;

禁用然后启用UITextField也有帮助,但它会突然将我的软键盘从一个更改为另一个

在设置textField
secureEntry
属性
NO
后,您需要调用
becomeFirstResponder
方法,它对我有效

试试这个

 textField.secureTextEntry = NO;
 if (textField.isFirstResponder){
    [textField becomeFirstResponder];
 }

他们都为Swift 4工作

self.isSecureTextEntry = false

if textField.isFirstResponder {
    textField.becomeFirstResponder()
    textField.layoutIfNeeded() 
}
@objc func tapOnLeftIcon(gesture: UITapGestureRecognizer) {
        let textRange = textField.selectedTextRange

        textField.isSecureTextEntry = !textField.isSecureTextEntry
        textField.selectedTextRange = nil
        textField.selectedTextRange = textRange
    }
我不得不添加
textField.layoutifneedd()
说明它是为Swift或use工作的

    let textRange = self.selectedTextRange
    self.selectedTextRange = nil;
    self.selectedTextRange = textRange
斯威夫特4号

self.isSecureTextEntry = false

if textField.isFirstResponder {
    textField.becomeFirstResponder()
    textField.layoutIfNeeded() 
}
@objc func tapOnLeftIcon(gesture: UITapGestureRecognizer) {
        let textRange = textField.selectedTextRange

        textField.isSecureTextEntry = !textField.isSecureTextEntry
        textField.selectedTextRange = nil
        textField.selectedTextRange = textRange
    }

可能是@Andrei的复制品完全成功了。你就是那个人