显示/隐藏密码 - 如何添加此功能? [英] Show/Hide Password - How can I add this feature?

查看:35
本文介绍了显示/隐藏密码 - 如何添加此功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了论坛,但看到的答案参差不齐,尤其是来自旧 Xcode 版本的答案.

我只是在输入了我在此您可以简单地使用此视图代替 SecureField.它里面有眼睛图标,所以在大多数情况下你不需要关心任何事情.

struct SecureInputView:查看{@Binding 私有变量文本:字符串@State 私有变量 isSecured: Bool = true私有变量标题:字符串初始化(_ 标题:字符串,文本:绑定<字符串>){self.title = 标题self._text = 文本}var主体:一些视图{ZStack(对齐:.trailing){如果是安全的{SecureField(标题,文本:$ 文本)} 别的 {文本字段(标题,文本:$ 文本)}按钮(动作:{isSecured.toggle()}) {图像(系统名称:self.isSecured?eye.slash":eye").accentColor(.gray)}}}}

将此视图复制粘贴到您的应用程序中,而不是 SecureField 只需使用 SecureInputView.

示例:SecureInputView("Password", text: $viewModel.password)

I've looked through the forums but I'm seeing mixed answers especially ones from an old Xcode version.

I only decided to add this after already typing up the code I have in this Photo.

How could I go about doing that? I was wanting the 'Eyeball' toggle implemented on the password field.

解决方案

You can simply use this view instead of SecureField. It has the eye icon inside, so for most cases you don't need to care about anything.

struct SecureInputView: View {
    
    @Binding private var text: String
    @State private var isSecured: Bool = true
    private var title: String
    
    init(_ title: String, text: Binding<String>) {
        self.title = title
        self._text = text
    }
    
    var body: some View {
        ZStack(alignment: .trailing) {
            if isSecured {
                SecureField(title, text: $text)
            } else {
                TextField(title, text: $text)
            }
            Button(action: {
                isSecured.toggle()
            }) {
                Image(systemName: self.isSecured ? "eye.slash" : "eye")
                    .accentColor(.gray)
            }
        }
    }
}

Copy paste this view into your app, and instead of SecureField just use SecureInputView.

Example: SecureInputView("Password", text: $viewModel.password)

这篇关于显示/隐藏密码 - 如何添加此功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆